Adding breaks in text-fields

Discussion in 'Web Design & Coding' started by Glaanieboy, Jun 25, 2003.

  1. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    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!
     
  2. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    I've always used str_replace

    Exmaple :
    PHP:
    $thinger  str_replace("\n""<br>""$thinger");
    Hope that helps ;)
     
  3. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    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
     
  4. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Stupid f00k that I am. So simple, so simple... Anyway, it works now. I can now continue working on my script :D Thank YOU!
     
  5. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Sorted :D
     
  6. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    damn it took me so long to type that (stupid addictive Freecell) SPeedY_B got in first
     
  7. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    And there also the SIMPLE(tm) way. :)
    Try the nl2br() function. ;)
     
  8. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    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. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    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 />.
     
  10. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    It goes 'raw' into the db and will be formatted when being read from the db. :)
     
  11. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    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.
     
  12. mhr

    mhr Guest

    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
     
  13. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    I'm pretty sure he wants to be able to HTML format the text. Links, fonts and so on.
     
  14. mhr

    mhr Guest

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

    Meher.
    France.
     
  15. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    Yeah, but it feels like that CSS construct will be more work than the nl2br() adds. Maybe it's just me.
     
  16. mhr

    mhr Guest

    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.