SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    finding a return and replacing it with a <br>

    I have designed a system so a user can input data in a form and it displays it on another page.

    The problem I am having is that when the user presses return in the text area and presses submit, the text is put into the database with a return, but when it appears on the html page it doesn't have a return. I know this is because it doesn't have a <br> in the text to indicate to the browser that there should be a return there. Does anyone know how I can use php to look through the form (im guessing you would use ereg) and then replace a return with a <br>?

    Help would be very much appreciated. Thankyou.

  2. #2
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $text= nl2br($text);

    use when extracting from table
    http://redgoals.com ... my site ... nuff said

  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem that I encountered using this function is that the br's are xhtml br meaning < /br> which has caused some problems with me.

    Therefore i used

    Code:
    $var = preg_replace("/\n/", "<br>", $var);
    and if i want to display the value again in the textarea i use

    Code:
    $var = preg_replace("/<br>/", "\n", $var);
    if you use nl2br you could use this to convert br back to \n

    Code:
    $var = preg_replace("<\s*\/*br>/", "\n", $var);

  4. #4
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be more irritating, but im stuck on how to use the code. Im not sure what to do with it exactly

    This is the code I am using to extract the data from the table:

    PHP Code:
       $result mysql_query("SELECT * FROM news WHERE id=$id",$db);

       
    $myrow mysql_fetch_array($result);
       
       
    printf("<table width=\"600\"><tr><td><center><b><font face=\"verdana, helvetica, arial\" size=\"2\" color=\"#c0c0c0\">%s</font></b></center><br>"$myrow["title"]);

       
    printf("<font color=\"#c0c0c0\" size=\"2\">%s</font><br><hr>"$myrow["main"]);

       
    printf("<font color=\"#c0c0c0\" size=\"1\">source:</font>&nbsp;<font color=\"ffffff\" size=\"1\">[</font><font color=\"c0c0cc\" size=\"1\">%s</font><font color=\"ffffff\" size=\"1\">]</font>"$myrow["author"]);

      
    printf("<font color=\"#c0c0c0\" size=\"1\">date posted:</font>&nbsp;<font color=\"ffffff\" size=\"1\">[</font><font color=\"c0c0cc\" size=\"1\">%s</font><font color=\"ffffff\" size=\"1\">]</font><br></td></tr></table>"$myrow["date"]); 
    If you could tell me what to do then I would be very happy as this thing has been bugging me for a while now. thanks again

  5. #5
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    Maybe try this:

    PHP Code:
    echo "<font color=\"#c0c0c0\" size=\"2\">".nl2br($myrow["main"])."</font><br><hr>"
    if you have just submitted from a textarea to the db

  6. #6
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, that doesn't seem to work, I get a parse error from that.

  7. #7
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I'm sorry it was untested. Try this instead

    PHP Code:
    $text nl2br($myrow["main"]);
    echo 
    "<font color=\"#c0c0c0\" size=\"2\">$text</font><br><hr>"

  8. #8
    SitePoint Member
    Join Date
    Mar 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    No im sorry that didn't work either it said :

    Fatal error: Call to undefined function: nl2br() in C:\redhunt\htdocs\index.html on line 176

    Im guessing the function needs to be defined??

  9. #9
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it's part of php you maybe using an older version.

    Try this instead:

    Code:
    $text = preg_replace("/\n/", "<br>", $myrow['main']);
    Then echo it.

  10. #10
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace() is way overkill for this! If you don't want to use nl2br() use str_replace():

    PHP Code:
    $str str_replace("\n""<br>"$str); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  11. #11
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <IGNORE ME.. TALKING RUBBISH AS USUAL>
    Last edited by weirdbeardmt; Mar 15, 2002 at 12:06.
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by weirdbeardmt
    AND you have to use nl2br BEFORE you insert into the database not when extracting from.
    Um. no, you should always keep your data as is, and only format it when you want to display it. That way if you need to share it with other apps that may not need or want a <br> in it, you will have no problems. Its the whole abstraction concept.

    BTW, do you think your title under your name is really appropriate for a multi-cultural board such as this. I for one find it slighlty offensive, even if that word is sometimes associated with cigarettes, it certianly has no place here.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp


    Um. no, you should always keep your data as is, and only format it when you want to display it. That way if you need to share it with other apps that may not need or want a <br> in it, you will have no problems. Its the whole abstraction concept.
    OK will retract that then.


    BTW, do you think your title under your name is really appropriate for a multi-cultural board such as this. I for one find it slighlty offensive, even if that word is sometimes associated with cigarettes, it certianly has no place here.
    The line is actually from a TV show in the UK so I guess unless you have seen it it won't make any sense. It certainly is not offensive in the way you suggest. On the offchance that people have taken it offensively I have changed it anyway.
    Last edited by weirdbeardmt; Mar 15, 2002 at 12:41.
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  14. #14
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as i've said before, nl2br() doesn't seem to work with \r only newlines (Mac). neither would sean's str_replace(). i replace any \r\n (Win) or \r (Mac) newlines with just \n before inserting into MySQL.

    Code:
    $txt = preg_replace('/\r\n|\r/', "\n", $txt);
    this ensures that nl2br() will recognize the newline. and for text that came from a Windows system, it saves 1 byte per newline in MySQL.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •