• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Adding breaks in text-fields

#1
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!
 

Khayman

I'm sorry Hal...
Political User
#3
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
 

SPeedY_B

I may actually be insane.
#8
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
 
#9
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 />.
 

X-Istence

*
Political User
#11
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.
 
M

mhr

Guest
#12
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
 
M

mhr

Guest
#14
Hello,
Using the <pre> or <xmp> tag allow HTML formatting through CSS or any other formating tag or I misunderstood your response.

Meher.
France.
 
M

mhr

Guest
#16
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,241
Members
89,016
Latest member
timpage