php:从字符串中删除多余的和标签

时间:2022-08-27 16:29:38

how can I remove excess <br> and &nbsp; tags from the start and end of a string?

如何删除多余的
和 字符串的开头和结尾的标签?

Thanks :)

2 个解决方案

#1


try this:

$str = preg_replace('{^(<br(\s*/)?>|&nbsp;)+}i', '', $str); //from start
$str = preg_replace('{(<br(\s*/)?>|&nbsp;)+$}i', '', $str); //from end

that also gets XHTML <br /> and <br/> forms

这也得到XHTML
和形式

#2


With preg_replace:

$str = "<br>some text&nbsp;"
$str = preg_replace('/(^(<br>|&nbsp)*)|((<br>|&nbsp)*$)/i', '', $str);

Didn't test it, but something like that should work.

没有测试它,但这样的东西应该工作。

#1


try this:

$str = preg_replace('{^(<br(\s*/)?>|&nbsp;)+}i', '', $str); //from start
$str = preg_replace('{(<br(\s*/)?>|&nbsp;)+$}i', '', $str); //from end

that also gets XHTML <br /> and <br/> forms

这也得到XHTML
和形式

#2


With preg_replace:

$str = "<br>some text&nbsp;"
$str = preg_replace('/(^(<br>|&nbsp)*)|((<br>|&nbsp)*$)/i', '', $str);

Didn't test it, but something like that should work.

没有测试它,但这样的东西应该工作。