Re: Question about replacing \r\n with <br>

From: Date: Sun, 23 Feb 2003 18:27:01 +0000
Subject: Re: Question about replacing \r\n with <br>
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-136746@lists.php.net to get a copy of this message
At 22:15 22.02.2003, Al said: --------------------[snip]-------------------- >I spent hours trying every User Notes in the PHP Manual for this simple >operation. e.g., > >$txt= preg_replace("\r\n", "<br>", $words); > >and this version > $txt = preg_replace("/(\015\012)|(\015)|(\012)/","<br />", $txt); > >I can substitute other characters and dec equivalents and the >substations just won't work for \r\n [or just \r or just \n] or "\015" >or "\15". > >And, I've tried using "10" and "010" and "13" and >"013". > >And nl2br doesn't work either. --------------------[snip]-------------------- nl2br() doesn't work? Then you don't have newline characters in your text. Maybe this is some Mac issue - I've heard that it might be possible that sometimes lines come separated by CR only. Try this: $asrch = array("/\n\r/s", "/\r\n/s", "/\n/s", "/\r/s", "/<br \/>/s"); $arrpl = array("<br />", "<br />", "<br />", "<br />", "<br />\n); $result = preg_replace($asrch, $arepl, $text); Should catch all possible variations, and finally substitute your "<br />" with a "<br />" followed by a newline character, to beautify the output. Note the "s" modifier that tells the regex parser to handle $text as a single line. If this is missing it will not find newline characters... -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#136746) next »