mridang_agarwal Posted October 24, 2008 Share Posted October 24, 2008 Hi. Could someone tell me how I can use the nl2br function to convert newlines to BR tags in text but I would like to exclude all the text that is placed within PRE tags. I tried to find some examples online but couldn't. Thanks guys. Quote Link to comment Share on other sites More sharing options...
Orio Posted October 24, 2008 Share Posted October 24, 2008 Check out the user comments in nl2br(). Someone made such a function. Orio. Quote Link to comment Share on other sites More sharing options...
mridang_agarwal Posted October 29, 2008 Author Share Posted October 29, 2008 Hi Orio, I couldn't find any such thing. This seems to be a pretty small thing to accomplish but I don't know why I cant seem to find a decent code snippet to do this. Anybody, any help? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 29, 2008 Share Posted October 29, 2008 I think this is the example on php.net Orio was referring to... however I'm not 100% sure. <?php function nl2br_pre($string, $wrap = 40) { $string = nl2br($string); preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1); for ($x = 0; $x < count($pre1[0]); $x++) { $pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]); $pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]); $pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/"; } return preg_replace($pre1[0], $pre2, $string); } ?> Quote Link to comment 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.