开发者

How can I reduce three or more repetitions of some text to only two?

开发者 https://www.devze.com 2023-03-24 06:04 出处:网络
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.

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 开发者_StackOverflow中文版/> in a row and I want to change that to just 2 <br /> in a row.

How can I do that?


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消