开发者

php: remove <p>, </p>, <br> and <br /> from beginning and end of string

help...

$chars = " \t\n\r\0\x0B";
$pattern = '('.implode('|'开发者_开发百科,array_map('preg_quote',array('<p>','</p>','<br />','<br>'))).')'."\b";
$data = trim(preg_replace('~'.$pattern.'$~i','',preg_replace('~^'.$pattern.'~i','',trim($data,$chars))),$chars);

That code is set to remove all <p>,</p>,<br> and <br /> from the beginning and end of a html string. But it is no working quite alright. Any ideas?


Why not just use something like this:

$subpattern = '(<(br|p)[^>]*>)';

$pattern = '~(^'.$subpattern.'|'.$subpattern.'$)~i';

Then, all you need to do is:

$data = trim(preg_replace($pattern, '', $data), $chars);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜