SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2002
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I str_replace the end of line character?

    I've looked throughphpmyadmins code and found these

    str_replace('\\r', "\015", $value);
    str_replace('\\n', "\012", $value);
    str_replace('\\t', "\011", $value);

    But none of them worked. I also tried
    str_replace('
    ', '\n\r', $value);

    But this was also unsuccessful. What I'm trying to do is format multilined sql dumps to a single line so they can easily be divided into an array and queried back but I can't seem to get rid of the line breaks which I want to preserve along the way (for readability of html kept in the db).

    Any ideas?

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How do I str_replace the end of line character?

    You have:
    str_replace('\\r', "\015", $value);

    try this
    // takes $value, strips out cariage returns and replace them with "015" and stores them back into $value
    $value = ereg_replace("\r", "015", $value);

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a sidenote:
    From php.net/str_replace
    If you don't need fancy replacing rules, you should always use this function (str_replace()) instead of ereg_replace() or preg_replace().
    Maybe this will work
    PHP Code:
    $value str_replace("\n"" "$value);
    // or
    $value str_replace("\r\n"" "$value); 
    -Helge

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I'm not 100% sure how MySQL works but maybe it would help to use a "fancy" () replacment rule to make sure that it cleans out all '\n', '\r', '\n\r' to pick it all up.

    But then again, that may not be needed since I don't know how MySQL works

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are we trying to get from and go to here? I'm lost

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Helge
    Just a sidenote:Maybe this will work
    PHP Code:
    $value str_replace("\n"" "$value);
    // or
    $value str_replace("\r\n"" "$value); 
    -Helge

    good point.. I just always used that from the beggining of my learning, and acctually didn't realize that was there.

    thanks.

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2002
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what worked for me.

    $text=ereg_replace("\r", "0150", $text);
    $text=ereg_replace("\n", "0120", $text);

    This got rid of mac and windows line breaks in the sql so I could export it in a one-query-per-line format to be similarly imported by other scripts.

    Thanks.


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
  •