1

I've got a text. I want to find out if a certain part of that text is repeated three or more times and replace that by only two repetitions.

For example, in the HTML code I'm looking at, there are 3 or more <br /> in a row and I want to change that to just 2 <br /> in a row.

How can I do that?

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Ayro
  • 473
  • 2
  • 5
  • 12
  • Can you please provide specific examples of what text you are starting with, and how you'd like to to end up looking? – OverZealous Jul 31 '11 at 05:15
  • abcd eeee fghklm oooooo In this text, I want to e and o write just twice.Like this abcd ee fghklm oo – Ayro Jul 31 '11 at 07:00

4 Answers4

2

Is this what you want?

<?php

$s='<br /><br />  <br />';

$s=preg_replace('#(<br />\s*<br />)(?:\s*<br />)+#', "$1", $s);
print($s);

?>

If there are more than 2 consecutive <br /> tags (not counting whitespace), delete all but the first two.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
1

Edit: As noted by Tim below, my original answer was altogether incorrect.

The correct regex for replacement would look like:

$s = preg_replace('/(.)\1{2,}/', '$1$1', $s);

It means: match any character once, then the same character (\1) at least twice more ({2,}), and replace the entire matched set with the first character, but only 2 times.

However, it might be that the above answers are probably closer to what you want.


For posterity, my original, incorrect regex looked like: /(.){3,}/ig

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • I could be wrong but I think this will duplicate the characters. Since `(.{3,})` would match say `aaaaa` and `$1 = aaaaa` the final output would be `aaaaaaaaaa`. – MitMaro Jul 31 '11 at 07:22
  • 2
    This is just plain wrong. It matches any run of three or more characters and replaces it with two instances of the last character matched. So it will transform `abcdefg` into `gg`. It seems that neither you nor whoever upvoted this actually tested this regex. If you're going for single characters (which is not what the OP needs), the regex should be `(.)\1{2,}`. Furthermore, the case-insensitive and global flags are useless here. (You need the global flag in JavaScript, not in PHP). – Tim Pietzcker Jul 31 '11 at 08:27
  • Tim, you are right, I was answering a question too late at night. I'll be more careful in the future, and update the answer now. – OverZealous Jul 31 '11 at 16:49
0

Not sure if it's possible to do this with a single regex. You probably need something like this:

$temp = preg_split('/<br \/>/', $input, 3);
if (count($temp) == 3) {
    $temp[2] = str_replace('<br />', '', $temp[2]);
}
$result = implode($temp, '<br />');

By the way: it's not a good idea to use regular expressions for HTML parsing

Community
  • 1
  • 1
user123444555621
  • 148,182
  • 27
  • 114
  • 126
0

If it is just <br /> you are trying to replace and not multiple patterns then this should work:

$s = preg_replace('/(<br />){3,}/', '<br /><br />');

If you need to match several different strings then this won't work.

MitMaro
  • 5,607
  • 6
  • 28
  • 52