views:

66

answers:

2

Seems simple enough but I'm having some issues, I tried using preg_replace:

preg_replace("<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?

+6  A: 
preg_replace("#<br />#", "", $string);

You needed a delimiter character. # works fine here, since it's not used in the regex.

Matthew Flaschen
people dont always close their breaks like they should so it should probably be something like `#<br[\s/]?>#`.
prodigitalson
+1, for mention of delimiters
alex
+1 The delimiters for strings is probably one of the most commonly unknown things in the language.
+5  A: 

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.

alex