views:

114

answers:

5

need a simple preg_replace to convert all <br> <br/> and all possible br combinations to <br />.

This needs to work in order so i can process a string ie: $output = preg_replace('', '<br />', $input)

Thanks everyone!

A: 

/< ?[bB][rR] ?\/? ?>/

dwo
This covers at least 95% of all combinations, so I do not unterstand the downvote.
dwo
+1 to even it out.
Pekka
A: 
$output = preg_replace('/< ?[bB][rR] ?\/? ?>/', '<br />', $input);
moustafa
breaks on `<br />`... plus you just copied this from dwo...
nickf
*sigh*... the commenting thing stripped out the extra space that was supposed to be in there. here's another one which it wouldn't work for: `<br clear="all" />`
nickf
+6  A: 

[Obligatory HTML parser comment]

If you're working with unknown and non-consistent HTML (as it sounds like you are), then put down the regex, you might hurt yourself. Finding a list of tags and altering a document is what HTML parsers were built for.

Learn the PHP DOM Methods and save yourself a lot of heartache.

nickf
Obligatory +1 following obligatory HTML parser recommendation
Brian Agnew
A: 

Try this pattern

<\s*[bB][rR]\s*\/?\s*>
akr
+2  A: 

One RegEx to rule them all:

$output = preg_replace('/<\s*br[^>]*>/i', '<br />', $input);
Pascal