SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple ereg_replace question

    Hi,
    For some reasons I don't know, when I display $text, I still have \n and \n\n on screen. I addslashes when inserting to database and stripslashes $text before the statements below, then displaying it. What's wrong with ereg_replace?

    // Strip out carriage returns
    $text = ereg_replace("\r","",$text);
    // Handle paragraphs
    $text = ereg_replace("\n\n","<P>",$text);
    // Handle line breaks
    $joketext = ereg_replace("\n","<BR>",$text);



    John

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh.. builtin function called nl2br which adds a <br> tag for each new line found is probably want you want to use.

    PHP Code:
    $text nl2br($text); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beats me, I'd just use nl2br(): http://php.net/nl2br

  4. #4
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do what was mentioned earlier.

    But a side note: if you're doing a simple search and replace (with no regular expressions involved) use str_replace() instead of ereg_replace().
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  5. #5
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. If in a form, I enter (type exactly letter by letter) the following 2 sentences:

    This is a \n first sentence
    This is a second sentence

    Note that at the end of the first sentence, I press "enter key" and then type the second sentence.

    Say $text contains the 2 sentences above, and then I press submit key to call x.php

    <?
    echo $text;
    $text = nl2br($text);
    echo $text;
    ?>

    The first echo displays:
    This is a \n first sentence This is a second sentence

    The second echo displays:
    This is a \n first sentence
    This is a second sentence

    nl2br works but it won't take care of \n that I typed in the first sentence (a slash and a letter n). How do I turn the first sentence into:

    This is a
    first sentence

    Thanks,
    John

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $text = str_replace("\\n","<br />",$text);

    Typing \n in a form isn't the same as \n in PHP. \n in a form is actually \\n (two separate characters).

  7. #7
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $text = str_replace("\\n","<br />",$text);
    Typing \n in a form isn't the same as \n in PHP. \n in a form is actually \\n (two separate characters).
    Thanks. It works. I don't why the form does that to \n (adding another slash)?

    BTW Why do we need "/" in <br /> ? I saw in some other threads have it too. But with or without it, it still works.

    Thanks,
    John

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its the new xhtml way of writing <br>, I guess it will become the standard in the future, so everyone is getting their code ready.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •