I am stripping HTML and replacing all <div>
and <p>
tags with <br />
tags. Problem is that I am left with random <br />
tags like
<br /><br /><br />
<br /><br />
<br /><br /><br /><br />
I am taking the body of an email and stripping tags like
$comment = strip_tags($comment,'<div><p>');
$comment = preg_replace("/<p[^>]*?>/", "", $comment);
$comment = str_replace("</p>", "<br />", $comment);
$comment = preg_replace("/<div[^>]*?>/", "", $comment);
$comment = str_replace("</div>", "<br />", $comment);
I want to be able to replace any instance of at least 2 <br />
tags next to each other with one single <br />
tag. At this point every <br />
will match exactly as I have shown, but...there is a possibility that they can vary like <br>
, <br />
so just need to make sure that I can replace any type of br tag (when there are at least 2 of them) with one single one when they are sequentially repeated next to each other.
The "possibility" of a br tag looking like <br>
is based on if it was already in the original HTML that I did not catch. I know I can do a str_replace("<br>", "<br />", $comment)
but I was hoping to shorten my code and not add more lines.
Any idea how to do this? Im sure there is regex involved and preg_replace
but not sure where to start.