preg_replace '

' with '
'?

2019-03-26 08:15发布

问题:

I have my code removing the <p> starting tags, but now I want to replace the ending </p> tags with line breaks. How can I do this?

This is what I have:

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

回答1:

use str_replace instead of preg_replace, so:

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


回答2:

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