开发者

replace <br> to new line between pre tag

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开发者_StackOverflow&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.


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜