I’m inserting HTML paragraphs (<p></p>) into a piece of text, like this:
$text = '<p>' . preg_replace("/(\n|\r|\r\n)+/i", "</p><p>", $text) . '</p>' ;
Which seems to work well, except I don’t want any paragraphs within <code></code> blocks since content within those blocks are pre-formatted (using a white-space:pre; style).
I’m not sure how best to handle this. I’ve tried to remove any such tags after the above line of code, but that’s causing me some trouble and I figure it would be much better not to insert them in the first place.
Is it possible and/or practical to make the exclusion in the regex above? If not, what else?
Thanks
Edit: Came up with this code based on Nameless’ answer below. It appears to work.
$chunks = preg_split("/(<code>.*?<\/code>)/is", $text, -1, PREG_SPLIT_DELIM_CAPTURE) ;
$text = '' ;
foreach($chunks as $chunk) {
if (preg_match("/^<code>/i", $chunk)) {
$text .= $chunk ;
} else {
$text .= '<p>' . preg_replace("/(\n|\r)+/i", "</p><p>", $chunk) . '</p>' ;
}
}
Well, it is possible with PCRE regex engine. Yet, highly irrational and resourse-heavy.
Using DOM is probably the best solution, if you can spend some additional RAM on this operation.
If not, you could split your string beforehand in chunks of
<code>…</code>and everything else, than use your regex on chunks not in<code>, than glue it back into string.