开发者

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

开发者 https://www.devze.com 2023-01-03 17:43 出处:网络
help... $chars = \" \\t\\n\\r\\0\\x0B\"; $pattern = \'(\'.implode(\'|\'开发者_开发百科,array_map(\'preg_quote\',array(\'<p>\',\'</p>\',\'<br />\',\'<br>\'))).\')\'.\"\\b\";

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

精彩评论

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