DonCullen Posted September 16, 2007 Share Posted September 16, 2007 Basically, I wrote a function that'd take a string, apply the nl2br function to it, but also leave any content contained in between pre tags alone. For example: Normal content/n Normal content/n <pre>Pre'ed content/n Pre'ed content/n Pre'ed content</pre>/n Normal content/n Normal content/n Would become: Normal content<br /> Normal content<br /> <pre>Pre'ed content/n Pre'ed content/n Pre'ed content</pre><br /> Normal content<br /> Normal content<br /> This is the function I wrote: #Applies nl2br to $text, but also makes sure to avoid applying the function to any content within pre tags function nl2brex($text) { if(!stripos($text, '<pre>', $prevpos)){ $text = nl2br($text); return $text; } $prevpos = 0; $finaltext = ''; while (strlen($finaltext) <= strlen($text)) { $preposition = stripos($text, '<pre>', $prevpos); $splittedstring = substr($text, $prevpos, $preposition); $splittedstring = nl2br($splittedstring); $finaltext .= $splittedstring.'***before pre***'; $afterpreposition = stripos($text, '</pre>', $preposition) + 6; $splittedstring = substr($text, $preposition, $afterpreposition).'***after pre***'; #msgbox($afterpreposition); #msgbox(strlen($text)); $finaltext .= $splittedstring; $prevpos = $afterpreposition; } return $finaltext; } The function, unfortunately, doesn't work correctly. For some reason, it isn't catching the </pre> tag, and appending the ***after pre*** to the end of the entire string, instead of right after the </pre> tag. The reason why I have the *** before/after pre *** appended is to assist in debugging, when I was trying to figure out why the function wasn't operating as expected. You can see the results of the function being applied here: http://bnetdocs.dementedminds.net/?op=packet&pid=237 Scroll down to view the first comment, and you'll see what I'm talking about. If there's a better/more efficient way to accomplish the task, I'm open to alternatives. Quote Link to comment https://forums.phpfreaks.com/topic/69533-solved-function-to-apply-nl2br-to-text-but-not-to-tags-not-working-right/ Share on other sites More sharing options...
php_joe Posted September 16, 2007 Share Posted September 16, 2007 I'm not sure how it would work, but couldn't you use nl2br() to change the lines into <br /> and then use preg_match() to replace the <br /> between the <pre> and </pre> back into \n? Quote Link to comment https://forums.phpfreaks.com/topic/69533-solved-function-to-apply-nl2br-to-text-but-not-to-tags-not-working-right/#findComment-349505 Share on other sites More sharing options...
wildteen88 Posted September 16, 2007 Share Posted September 16, 2007 Try: <?php $text = 'Original text with newlines! <pre>my text should not have <br />\'s!</pre> Ohh! More'; function restore_pre($text) { $text = str_replace(array("<br />\r\n", "<br />\r", "<br />\n"), "\n", $text); return '<pre>' . htmlentities($text) . '</pre>'; } // add newlines $text = nl2br($text); // restore pre tags to orginal state. $text = preg_replace('/<pre>(.*)<\/pre>/ies', "restore_pre('$1')", $text); echo $text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/69533-solved-function-to-apply-nl2br-to-text-but-not-to-tags-not-working-right/#findComment-349610 Share on other sites More sharing options...
DonCullen Posted September 16, 2007 Author Share Posted September 16, 2007 Thanks wildteen. Found a solution. But I modified it to include some of your code. Here's my solution: #Applies nl2br to $text, but also makes sure to avoid applying the function to any content within pre tags function nl2brex($text) { $text = nl2br($text); $text = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $text); return $text; } # Remove paragraphs and breaks from within any <pre> tags. function clean_pre($text) { $text = str_replace(array("<br />\r\n", "<br />\r", "<br />\n"), "\n", $text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/69533-solved-function-to-apply-nl2br-to-text-but-not-to-tags-not-working-right/#findComment-349691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.