0

<br>たとえば、2 回以上繰り返される複数のタグを削除したい:

This is text blah blah...
<br><br><br><br><br><br>Another text here

次のようにする必要があります。

This is text blah blah...
<br><br>Another text here

私はこれを持っています:

$str = preg_replace('#(<br\s?/?>)+#', '<br><br>', $str );

<br>しかし、これも単純なタグを 2に置き換えますbr。これの正規表現を変更するにはどうすればよいですか?

ありがとう

4

2 に答える 2

1

これを試して:

$str = 'This is text blah blah...
<br><br><br><br><br><br>Another text here';

echo preg_replace('#(\s*<br\s*/?>\s*<br\s*/?>\s*)+#', '<br><br>', $str );

出力:

This is text blah blah...
<br><br>Another text here

デモ

更新: @M42 の指摘に感謝します:

echo preg_replace('#(\s*<br\s*/?>\s*){2,}#', '<br><br>', $str );
于 2013-09-10T11:03:44.787 に答える