Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

frustrating nl2br()

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

frustrating nl2br()

Postby mynmonic » Thu Jun 12, 2003 10:24 am

There is a weird problem when i m using nl2br().

I simply input text in the text area of a html form, after that i try to write that text as a record into a text file. Normally I won't type return key for the text input. But when I need to do this, ok I should use nl2br() before i write it into the text file, so I can change any return key in hit in the text into <br>, and keep the whole text as a single line to save in that text file.

If the text I input is..
"This is line one
and this is line two"

After this text passed to nl2br(), it should return..

"This is line one<br>and this is line two" right?

But what I can see is..
"This is line one<br>
and this is line two"

So it didn't replace the new line character into <br>, instead, it's just insert the <br> before the return character. What's the problem? Is that my concept on nl2br() wrong?

Please advice, thanks
mynmonic
New php-forum User
New php-forum User
 
Posts: 18
Joined: Tue Nov 26, 2002 3:35 am
Location: Hong Kong

Postby Redcircle » Fri Jun 13, 2003 5:13 am

nl2br() does not remove the \n character it simply adds the the break. To get what you want use
$output = preg_replace("/(\r\n|\n|\r)/", "[br]", $input);

change [] to < >
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby mike » Fri Jun 13, 2003 5:12 pm

Simply use: $text = str_replace("\n","<br>",$text);

always working fine.
User avatar
mike
New php-forum User
New php-forum User
 
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens

Postby mynmonic » Mon Jun 16, 2003 7:21 am

Thanks, I use str_replace() and it is work now. But I swear I have try the str_replace() long time ago before I ask the question here this time, I have no idea why it didn't work at that time, now I type exactly the same syntax and I got what I want.

btw what's the different to use preg_replace() or str_replace()?
and what is the /n/r or /r/n means?
mynmonic
New php-forum User
New php-forum User
 
Posts: 18
Joined: Tue Nov 26, 2002 3:35 am
Location: Hong Kong

Postby Joan Garnet » Mon Jun 16, 2003 1:23 pm

mynmonic wrote:btw what's the different to use preg_replace() or str_replace()?
and what is the /n/r or /r/n means?

Use preg_replace() if you need to use complex replacing rules, otherwise just use the straight forward str_replace().

***some PHP predefined escape sequences***
\r -- carriage return
\n -- newline
\t -- tab
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby mike » Sun Jun 22, 2003 3:51 pm

There is a long history with "string replacements". Just use str_replace() the way I told on the previous post and you want have any problems ever :D

There are a few dozens of differences between these functions and everyone of them has some incompatibilities due to different OS and filesystems.
User avatar
mike
New php-forum User
New php-forum User
 
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.