replace new line?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

replace new line?

Post 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....
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

Post by ReDucTor »

$str=str_replace("\n","",$str);

Just as you said..This should replace \n with nothing
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Although you may need to replace \r\n or \n or \r depending on your system.

Mac
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

Post 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);
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

is it faster to use an array rather than multiple str_replace() ?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

It's easy'r. Then make 3 lines of code with the same statement.
Post Reply