Hi everyone I had previously posted about a <br> getting inserted at the beginning of my text and we got that fixed. (here is my previous post with the code) I changed the code as suggested from
$x = preg_replace("/<br>/i","\n",$x);
to
$x = preg_replace("/^<br(\/|)>/i","\n",$x);
which worked to stop the <br> from being inserted at the beginning but now it is inserting one at the end. How do I stop that from happening?
SOLUTION
$x = preg_replace("/^<br(\/|)>/i","\n",$x);
Jacks solution— //$x = preg_replace('#^<br(\/|)>+|<br(\/|)>+$#i', "\n", $x);
My fix in addition to the original preg_replace
$x= preg_replace("/(^)?(<br\s*\/?>\s*)+$/","\n", $x);
UPDATE
it is no longer adding <br> each time it is submitted but there are now two<br> after the text that only show up in the database and are not brought out in the editor. I need to get rid of these breaks!
This should do it, matches
<br>,<br/>or<br />at the start or end:The tag is matched by:
* Literal
<br* Optional spaces followed by a forward slash
The
|in the middle is used to denote an alternative condition (i.e.OR).Edit
Instead of
<br(\s*/)?>you can also write<br(\/|)>that you had before.Edit 2
Multiple occurrences can be matched by just adding
+behind each pattern: