SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Double results when using str_replace

    I am confused why the following code returns two Xs between each paragraph. I was expecting only one.

    $paragraphs = str_replace("\n", X", $paragraphs);

    So then I figured there must be two line breaks in the text coming from the database. But when I tried the following code, there was no X at all between each paragraph.

    $paragraphs = str_replace(\n\n", X", $paragraphs);

    Im clearly not understanding about something regarding line breaks. If it makes any difference, Im working on a Mac, and I think they handle line breaks differently than a PC.
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by busboy View Post
    I am confused why the following code returns two X’s between each paragraph. I was expecting only one.

    $paragraphs = str_replace("\n", “X", $paragraphs);

    So then I figured there must be two line breaks in the text coming from the database. But when I tried the following code, there was no X at all between each paragraph.

    $paragraphs = str_replace(“\n\n", “X", $paragraphs);

    I’m clearly not understanding about something regarding line breaks. If it makes any difference, I’m working on a Mac, and I think they handle line breaks differently than a PC.
    Chances are there were other bits stored in between the multiple \n's, which is why your second attempt didn't insert any X's.

    When dealing with line breaks, I like to use preg_replace, because you can generally define a catch for all systems by using
    PHP Code:
    $paragraphs preg_replace("/(\r\n|\r|\n)/""X"$paragraphs); 
    It will attempt to find \r\n together, or a single \r or a single \n, thus covering the typical Operating System end lines.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You can also use the PHP_EOL constant. It will give you the line ending for the current platform.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It’s still not working for me, and I even tried adding two \n’s like this,
    /(\r\n|\r|\n\n)/
    , in your preg_replace example. So that I can get this resolved, is there a way to force the text to show these “hidden” bits that are in the paragraph text? Thanks cpradio!
    Convert your dollars into silver coins. www.convert2silver.com

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,806
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by busboy View Post
    I am confused why the following code returns two X’s between each paragraph. I was expecting only one.

    ... text coming from the database.
    Have you tried echoing nl2br( $paragraphs );


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
  •