2

I want to convert

<p>Code is following</p>
<pre>
&lt;html&gt;<br>&lt;/html&gt;
</pre>

to

<p>Code is following</p>
<pre>
&lt;html&gt;
&lt;/html&gt;
</pre>

I don't know how to write regular expression for replace between pre tag in PHP.

I tried this code Replace newlines with BR tags, but only inside PRE tags

but it's not working for me.

Community
  • 1
  • 1
saturngod
  • 24,649
  • 17
  • 62
  • 87

4 Answers4

5

Which answer are you using code from?

Assuming it was the accepted answer, just reverse the preg_replace() line as follows;

$parts[$idx] = preg_replace('#<br\s*/?>#', "\n", $part);
TheDeadMedic
  • 9,948
  • 2
  • 35
  • 50
0

You shouldn't use regex to match html tags because it is theoretically impossible.

There are some php librarys for html parsing out there, a quick search on google showed this. http://simplehtmldom.sourceforge.net/

Try to get the code between the "pre" tags and use a simple regex on this.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 2
    Come on. That's simply isn't true. For something as simple as replacing a
    tag, a quick bit of regex is just what the doctor ordered. He is not *parsing* HTML, he is just replacing a
    tag with a new line! I challenge you to find a HTML file where you can't reliably replace
    by a new line with regex alone.
    – Sylverdrag Jun 12 '10 at 11:04
  • 1
    Since the task is to replace
    only in
     tags I asume that the content comes from user input. And you don't know the text the user enters so you cannot reliably desing a regex that work 100% of the time. i.e. the user could insert some 
     tags himself.
    
    Also have a look here: http://stackoverflow.com/questions/2171817/qt-regex-matches-html-tag-innertext
    – Jürgen Steinblock Jun 12 '10 at 13:20
  • (?<=
    ]*?>(?!?pre>).*)(
    ) - Now, show me what kind of wacko HTML will prevent me from matching
    inside a
     tag with this regex. Don't tell me, show me. This expression will not break in many cases that would throw even the most flexible HTML parsers off. I keep reading the claim that regex can't work on HTML, but no evidence ever seem to be provided. Just because you can't PARSE HTML with regex doesn't mean you can't safely extract bits and pieces. If you look inside the source code of the .NET HTML parser library...it's full of regex!
    – Sylverdrag Jun 12 '10 at 14:53
  • 1
    I don't want to debate about this forever but try your regex with this snippet: http://pastebin.com/nRX4rYEG Without knowing the rules of the language there are always some backdoors. A regex that works 99.9% of the time is often a good choise (and I often do it myself. for instance if I want to match ip addresses I sometimes use (\d+\.){3}\d+ because that get's the job done even if it's wrong). But in the web you can't trust the input users make. And a big number of XSS exploits happen because developers don't do properly user input checking (partly with wrong regex statements). – Jürgen Steinblock Jun 13 '10 at 15:13
  • Oups, I forgot a non capturing group. Should have been (?<=
    ]*?>(?:(?!?pre>).)*)(
    ). That would work, except for the bit in the HTML comment (since the goal is to fix the display inside
     elements, it wouldn't matter). In the present context, replacing a 
    with a line break, regex is a pretty safe and efficient way to do the job. The consequences for missing a
    or replacing one
    too many are pretty much nil. Anyway, I think we agree on the main points. There is a point past which using regex is more trouble than it's worth.
    – Sylverdrag Jun 14 '10 at 08:41
0

Try this:

$newtext = preg_replace('@<pre>.*<br[/>]*?>?</pre>@si','\n',$text);
turbod
  • 1,988
  • 2
  • 17
  • 31
-1
if (preg_match("/<pre>.*(<br(|\s*\/)>).*<\/pre>/m", $str)) {
    $str = preg_replace("/(<br(|\s*\/)>)/", "\n", $str);
}

Works just the same. Replaces <br>, <br/>, <br /> only when found inside <pre>...</pre>

N. Lucas
  • 412
  • 3
  • 12
  • This will replace ALL `br` tags if there are any `pre` tags in the text that contain `br` tags, not just the ones inside the `pre` tags, which is not what OP wants, because he wants only the ones inside of the `pre` tags to be replaced, not the ones outside of them too. – Carter Pape Aug 19 '13 at 19:11