How to remove all break tags from a string in PHP?
Seems simple enough but I'm having some issues, I tried using preg_replace:
preg_re开发者_JAVA技巧place("<br />", "", $string);
But what I get is this <>
in place of the <br />
in the string when outputted. The break tags in the string will always be in this format not in any of these:
<br/>
<br>
<BR />
etc.
so what's the error I'm making?
preg_replace("#<br />#", "", $string);
You needed a delimiter character. #
works fine here, since it's not used in the regex.
In addition to what Matthew Flaschen said, you could probably get away with simply str_replace()
as you are not using any of a regex's power there.
Alternatively you could match different types of <br />
like so (using a regex this time)...
#<br\s?/?>#
This will match <br/>
, <br>
, <br />
. Add the i
flag to make it match in a case insensitive manner (or use str_ireplace()
if not using the regex).
Also, if you are doing any more HTML manipulation than this, consider a parser.
精彩评论