Adding breaks in text-fields

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
I have been playing around with <TEXTAREA>'s and I have one problem:
I have made a PHP-script (for submitting news) that stores the value in <TEXTAREA> in a MySQL database. hen I have 'enter's (new lines) in the textfield, it is send as a "Here comes%0D%0Athe new line" where %0D%0A is representing the enter. MySQL stores it ok, when I do a SELECT * ... I see the text exactly as I entered it. But, when I call the record with another script and display it on screen, I see it as one long string, with no breaks at all. Logical, because the browser only breaks the line when there is not enough screen width or when there is a <BR>. What I want is at the place where a break is present, replace that break by <BR>, so that it correctly shows up in the browser.
I tried it with a 'strtr' where I use an array of ("%0D%0A" => "<BR>"). It should replace any occurance of %0D%0A with <BR>, but it doesn't.
I know it's possible, I see that forum software does that all the time, but how do they do that?

I hope anyone can understand what I mean, it is very difficult for me to explain in english, because it isn't my native language.
For an example of what I mean, go here (will be up for at least a couple of hours):
http://212.64.24.229/shout/index2.php . There is a Submit News link present, so you can try it if you want.
Thank you for your time!
 
I've always used str_replace

Exmaple :
PHP:
$thinger  = str_replace("\n", "<br>", "$thinger");

Hope that helps ;)
 
have you tried

str_replace();

which replaces all occurrences of the search string with the replacement string

works like $newtext=str_replace('%0D%0A','<BR>;',$oldtext);

havn't actualy tried it with your partuclar problem, but its what i use to replace text
 
Stupid f00k that I am. So simple, so simple... Anyway, it works now. I can now continue working on my script :D Thank YOU!
 
damn it took me so long to type that (stupid addictive Freecell) SPeedY_B got in first
 
And there also the SIMPLE(tm) way. :)
Try the nl2br() function. ;)
 
Note: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'.
w00t! XHTML goodness, just like ma used to bake :D

Depends if he wants to replace the value that SQL is printing, or replce the line breaks before entering it into the db.. either way will work :D
 
I'd say do it after you take the data out. This way the data is still "intact" and can be used for something that would not require (or even allow) the (X)HTML formating with <br />.
 
It goes 'raw' into the db and will be formatted when being read from the db. :)
 
i always make sure i addslashes() and make it safe so that noone can screw with my MySQL queries, formatting gets done on the fly once it gets output to the browser.
 
HI,
An simple and cpu-saving solution is to use the HTML tag <pre></pre> which is designed for that.
If the input can/may comes with < or >, there is also the forgotten tag <xmp></xmp> which tels the browser not to display the content as HTML.

Meher.
France
 
I'm pretty sure he wants to be able to HTML format the text. Links, fonts and so on.
 
Hello,
Using the <pre> or <xmp> tag allow HTML formatting through CSS or any other formating tag or I misunderstood your response.

Meher.
France.
 
Yeah, but it feels like that CSS construct will be more work than the nl2br() adds. Maybe it's just me.
 
Let's try an example to show my point of view.
Image the entered text is
First line is short
Second line is < then the third but > than the first one
The third line is the longuest of them three, just to demonstrate my point of view.
even with nl2br you will not be able to reproduce the text, if you dont Encode the entire line (replace > by &gt; and so on).
but if you write
<h1><font color="red"><xmp><? string_get_raw_from_db ?></xmp></font></h1>
You get what you expect and in the format you chose (h1 in red in my example.)

Did I get it right ?
Meher.
France.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back