preg_replace'<p> </p>'和'<br/>'?

时间:2011-03-14 02:43:31

标签: php preg-replace

我的代码删除了<p>起始代码,但现在我想用换行符替换结束</p>代码。我怎么能这样做?

这就是我所拥有的:

$content = 'This is the content';
$newcontent = preg_replace("/<p[^>]*?>", "", $content);
$newcontent = preg_replace("</p>", "<br />", $newcontent);

2 个答案:

答案 0 :(得分:29)

使用str_replace而不是preg_replace,所以:

$content = '<p>This is a new content for missing slash</p>';
$newcontent = preg_replace("/<p[^>]*?>/", "", $content);
$newcontent = str_replace("</p>", "<br />", $newcontent);

答案 1 :(得分:6)

$content = preg_replace('#<p(.*?)>(.*?)</p>#is', '$2<br/>', $content);