adamlacombe Posted September 20, 2009 Share Posted September 20, 2009 Does anyone know of a function that will take and replace the \r\n to <br>? I checked the manual and found nl2br() but it does not seem to work... this is what my functions file looks like: <?php function unhtmlentities($string) { // replace numeric entities $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string); // replace literal entities $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } 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); } function clean_up($string){ $string=mysql_real_escape_string(strip_tags(htmlentities($string, ENT_QUOTES))); $string = html_entity_decode($string); $string = unhtmlentities($string); $string = nl2br_pre($string); return $string; } ?> any help would be great! Thanks in advanced, Adam Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 20, 2009 Share Posted September 20, 2009 nl2br() will do just that my reg expresions is not great.. but are you counteracting it here: $pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]); Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 umm I have no idea lol. I found that in the manual, I have been trying anything and everything, im pretty good with PHP but when it comes down to this part I have no idea at all lol Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted September 20, 2009 Share Posted September 20, 2009 I suck with regular expressions so I use str_replace("\r\n", "<br>", $string); Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 yeah I tired using str_replace too... I dont get why that wouldn't work either.. Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 20, 2009 Share Posted September 20, 2009 <?php function unhtmlentities($string) { // replace numeric entities $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string); // replace literal entities $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } function nl2br_pre($string, $wrap = 40) { 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); } function clean_up($string){ $string=mysql_real_escape_string(strip_tags(htmlentities($string, ENT_QUOTES))); $string = html_entity_decode($string); $string = unhtmlentities($string); $string = nl2br_pre($string); $string = nl2br($string); //moved to here from nl2br_pre(); return $string; } ?> A guess, try making nl2br($string); the last call to action? Only stabbing in dark... with out understanding the regular expressions being used there :-\ Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 nope that didn't work either. Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted September 20, 2009 Share Posted September 20, 2009 nope that didn't work either. hmmm that's weird, I hate messing with line breaks too. have you tried replacing \n without the \r? worth a shot if nothing else is working lol Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 20, 2009 Share Posted September 20, 2009 have you tried replacing \n without the \r? worth a shot if nothing else is working lol lol, or take it further $string = preg_replace('/\r\n|\r|\n/', "<br>", $string); // I used this the other so still remember what it does haha Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 yay!! it works! I can finally go to bed now lol. Thanks!! Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted September 20, 2009 Share Posted September 20, 2009 nice, i'll have to save that snippet for when I have this problem again, I know I will =) Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 ok I thought everything was good, but I woke up this morning and went to fix the same problem with another site and I added that code: <?php function protect($string) { $string = mysql_real_escape_string($string); return $string; } function unhtmlentities($string) { // replace numeric entities $string = preg_replace('~&#38;#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); $string = preg_replace('~&#38;#([0-9]+);~e', 'chr("\\1")', $string); // replace literal entities $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } function nl2br_pre($string, $wrap = 40) { 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); } function clean_up($string){ $string=trim(mysql_real_escape_string(strip_tags(stripslashes(htmlentities($string, ENT_QUOTES))))); $string = html_entity_decode(unhtmlentities(nl2br_pre(nl2br($string)))); $string = preg_replace('/\r\n|\r|\n/', "<br>", $string); $string = str_replace('\r\n', "<br>", $string); return $string; } function numeric_only($string){ $string=preg_replace("/[^0-9]/","",$string); return $string; } ?> to the functions file and now im getting this error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/media/test/includes/functions.php:48) in /home/media/test/index.php on line 5 on one server its fine but the other one it gives me that error. any idea what could be causing that error? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 what's on line 40 to 50 of functions.php and what's the first 10 lines of index.php Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 lines 40-50 (functions.php): $string = str_replace('\r\n', "<br>", $string); return $string; } function numeric_only($string){ $string=preg_replace("/[^0-9]/","",$string); return $string; } ?> then first 10 lines (index.php): <?php include ('includes/db.php'); include ('includes/functions.php'); include ('includes/global.php'); session_start(); if($sitestatus == online || $_SESSION['type'] == 1){ if($sitestatus == offline){ Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 Do you have any white space at the end of functions.php ? for example a space/return after the ?> ie "?> " Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 that was it! why would that cause an error?.. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 20, 2009 Share Posted September 20, 2009 okay here's the error Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/media/test/includes/functions.php:48) in /home/media/test/index.php on line 5 Now functions.php is outputting a space, now as soon as an output is made, you can't send a header, and session_start(), need to make a header call, thus it fails see HEADER ERRORS - READ HERE BEFORE POSTING THEM Quote Link to comment Share on other sites More sharing options...
adamlacombe Posted September 20, 2009 Author Share Posted September 20, 2009 oh ok. Thanks for the help! 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.