php - preg_replace - Replacing <br> with nothing in <pre> tags

969

I have the following:

$newtext = preg_replace('@<pre>.*<br[/>]*?>?</pre>@si','',$text);

and I want it to replace all<br>, <br />, or <br/> tags with nothing when between<pre> tags, but it isn't working. What's wrong with it?

435

Answer

Solution:

Your problem is that you're trying to use regular expressions to do a parser's job. Even if you get it mostly working, you'll be finding edge cases where things break down the road.

See: RegEx match open tags except XHTML self-contained tags

People are also looking for solutions to the problem: php - GZip Compression For JQuery Without Server Access

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.