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>' ;
}
}
Line one
Line two
Line three
Line four
". And I know CSS is for styling, but the HTML still tells CSS where to apply those styles. – Aug 20 '11 at 15:04