I want to convert
<p>Code is following</p>
<pre>
<html><br></html>
</pre>
to
<p>Code is following</p>
<pre>
<html>
</html开发者_StackOverflow>
</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.
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);
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.
Try this:
$newtext = preg_replace('@<pre>.*<br[/>]*?>?</pre>@si','\n',$text);
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>
精彩评论