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. Link to comment https://forums.phpfreaks.com/topic/129909-convert-new-lines-to-br-exclude-pre-tags/ 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. Link to comment https://forums.phpfreaks.com/topic/129909-convert-new-lines-to-br-exclude-pre-tags/#findComment-673606 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? Link to comment https://forums.phpfreaks.com/topic/129909-convert-new-lines-to-br-exclude-pre-tags/#findComment-677195 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); } ?> Link to comment https://forums.phpfreaks.com/topic/129909-convert-new-lines-to-br-exclude-pre-tags/#findComment-677206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.