Re: Simple String Replace Question

From: Date: Sun, 25 Feb 2001 23:32:21 +0000
Subject: Re: Simple String Replace Question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-41615@lists.php.net to get a copy of this message
From: "Jeff Oien" <jeff@webdesigns1.com> > That almost works. The two \n in a row are on new lines. > So it's > \n > \n > intead of \n\n. If that makes any sense. > Jeff Oien No, that doesn't make any sense whatsoever :) A \n *is* a new line. I can only guess you're getting confused because there's \r's as well as \n's in the string. Try this: <?php $str = "abc\r\ndefg\r\n\r\nxyzpqr\r\njklmno"; $str = ereg_replace("([^\r\n])\r\n([^\r\n])", "\\1 \\2", $str); echo $str; ?> Or alternatively: <?php $str = "abc\r\ndefg\r\n\r\nxyzpqr\r\njklmno"; $str = ereg_replace("\r", "", $str); $str = ereg_replace("([^\n])\n([^\n])", "\\1 \\2", $str); echo $str; ?> A CR is a carriage return, a LF is a line feed or newline. \r = CR, \n = LF. Unix files use only LF (\n) for new lines, whereas MS DOS/Windows uses CRLF (\r\n) and Mac uses just CR (\r). Cheers Simon Garner

« previous php.general (#41615) next »