Forum Moderators: coopster

Message Too Old, No Replies

put text into one line of code

         

omoutop

12:41 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi all, i need your advice.

In one application of mine, I allow the users to add some text (400 chars max). I strip slashes and tags before entering to the database.

In the user-end, they may hover their mouse over some small icon, and the text appear (with a combo of javascript and dhtml) into a nice looking alt-box.

Now the problem: the javascript requires the text to be all in one line. The text may have full html formating but must all be in one line.

*wrong example*
this is
wrong example

*correct example*
this is <br> correct example

How can I do this with php? I can use nl2br() to change the escaping line into a simple <br>, but how can I force the text to appear all in one line?

Hope i made myself clear

whoisgregg

2:37 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand correctly, I think all you need is
str_replace("\n", " ", $text);
?

omoutop

2:50 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No... Using str_replace the way you suggest, will delete my line breaks.

What i want is to maintain the line breaks, but format the text to appear in 1 line (in source code)

example (original):
this is some text<br>
in multiple lines

(to become)
this is some text<br>in multiple lines

DrDoc

3:04 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$foo = preg_replace("/\r?\n/", "<br>", $foo);

This will completely replace the newline characters with a break, and the new string will be a single line.

omoutop

3:22 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



DrDoc you are sweet.... :)

Exactly what i needed...

thanks a lot

and Merry Christmas