Question

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!

Was it helpful?

Solution

One RegEx to rule them all:

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

OTHER TIPS

[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.

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

Try this pattern

<\s*[bB][rR]\s*\/?\s*>
$output = preg_replace('/< ?[bB][rR] ?\/? ?>/', '<br />', $input);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top