Page 1 of 1
replace new line?
Posted: Sun May 11, 2003 11:50 am
by CONFIQ
Did any of you try to replace new line?
I know for nl2br, it just adds <br /> after \n.
I've try with str_replace("\n",'<br>',$str) but it's same as nl2br()
I just need to replace \n with something else....
Posted: Sun May 11, 2003 11:59 am
by ReDucTor
$str=str_replace("\n","",$str);
Just as you said..This should replace \n with nothing
Posted: Sun May 11, 2003 2:14 pm
by twigletmac
Although you may need to replace \r\n or \n or \r depending on your system.
Mac
Posted: Sun May 11, 2003 5:57 pm
by CONFIQ
you are right...
as far as i know,\r\n is for windows? \n for *nix and \r is for MAC?
Anyway i use this
str_replace("\r\n","<br />",$str);
str_replace("\n","<br>",$str);
str_replace("\r","<br />",$str);
Posted: Sun May 11, 2003 8:52 pm
by lc
I've always just used
$str = ereg_replace("\r\n","<br>",$str);
which works fine on both my windoze server and my host's unix system. But 1 or 2 people have had problems with my scripts on systems where for some reason \r\n was seen as 2 newlines.
Posted: Mon May 12, 2003 3:01 am
by twigletmac
lc wrote:I've always just used
Code: Select all
$str = ereg_replace("\r\n","<br>",$str);
You really don't need to use ereg_replace() for that though as you're not using a regular expression - str_replace() would be a better choice.
CONFIQ wrote:Anyway i use this
Code: Select all
str_replace("\r\n","<br />",$str);
str_replace("\n","<br>",$str);
str_replace("\r","<br />",$str);
You could also do:
Code: Select all
str_replace(array("\r\n", "\n\r", "\n", "\r"), '<br />', $str)
Mac
Posted: Mon May 12, 2003 6:05 am
by Coco
is it faster to use an array rather than multiple str_replace() ?
Posted: Mon May 12, 2003 6:24 am
by []InTeR[]
It's easy'r. Then make 3 lines of code with the same statement.