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

Jared picture Jared · Mar 14, 2011 · Viewed 23.3k times · Source

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);

Answer

Richard Rodriguez picture Richard Rodriguez · Mar 14, 2011

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);