Results 1 to 12 of 12

Thread: str_replace and <br> *resolved*

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    str_replace and <br> *resolved*

    on page1.php, I have a <textarea>. The user can put in anything there, even new line characters. I want to use the str_replace function to replace any occurences of new line characters with "<br>"

    How can I do this? (I'm guessing that vbCrlf isn't the correct one)
    Last edited by mendhak; Feb 6th, 2003 at 06:07 AM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I always do:

    Code:
    str_replace("\n", "<br>", $text);
    But I've seen other people throw in combinationes of "\r\n"
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Aha... \n. This seems so familiar

    I understand the \n must be newline. What is \r though?

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by mendhak
    Aha... \n. This seems so familiar

    I understand the \n must be newline. What is \r though?
    \r is a carriage return. There's also an nl2br() function:

    string nl2br(string string)


    Returns string with '<br />' inserted before all newlines.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thanks man.

  6. #6
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Yeah I think you have to take into account that Linux, Mac and Windows all handle newlines differently, so just doing a str_replace won't always get it right (or at least that's my understanding of it).

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    that is correct.

    \r = carrioage return and will work in windows
    \n = newline and most of the time won't work in windows
    \t = tab (I believe)

    that is why most people use both.

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    \n = newline and most of the time won't work in windows
    Wont work in Windows? I've never had a problem using newlines in Windows...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    that is what I said.... most of the time it does, but that is why they add \r to it to make sure.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    that is what I said.... most of the time it does
    No you didn't...

    Originally posted by phpman
    most of the time won't work in windows
    Is what you said.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    actually it is for the headers where it messes up.

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    actually it is for the headers where it messes up.
    Actually, my sack is in the back, I thank you.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width