我认为自己仍然是正则表达式的新手,并面临以下挑战:
我的用户发布的内容末尾包含一个或多个"换行符"。这些"换行符"<p><br></p>
标签之间具有不同数量的空格。有时,每个段落中都有不止一个<br>
。一些例子:
<p>
<br>
</p>
<p>
<br>
</p>
<p><br> <br>
</p>
<p>
<br>
</p>
如何从每条内容的末尾删除这些段落,同时删除包含的<br>
、空格、换行符和制表符?
<?php
$strings[] = 'foo<p>
<br>
</p>';
$strings[] = 'foo<p>
<br>
</p>';
$strings[] = 'foo<p><br><br>
</p>';
$strings[] = 'foo<p>
<br>
</p>';
foreach($strings as $string){
// 's* matches any number of whitespace characters (" ", 't, 'n, etc)
// (?:...)+ matches one or more (without capturing the group)
// $ forces match to only be made at the end of the string
$string = preg_replace("/(?:<p>'s*(?:<br>'s*)+<'/p>'s*)+$/", "", $string);
echo $string."'n---'n";
}
输出为:
噗噗��
---
噗噗��
---
噗噗��
---
噗噗�� ---