Results 1 to 7 of 7

Thread: Is it possible to do a search & repace on hard returns within a field?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Is it possible to do a search & repace on hard returns within a field?

    I imported a lot (35k) of records from a FoxPro database into a mySQL table. When I display the records using html, the data in the notes field all runs together like a huge run-on sentence which makes it difficult to read. In other words, it ignores the hard returns (or linefeeds) in the field. The hard returns are invisible so I don't know how to reference them.

    I'm wondering if there is a way to either:
    1) replace the hard returns with <br>'s
    2) format the field so that it appears the way it looks in phpMyAdmin
    3) instruct html to preserve the formatting somehow
    4) get the data out of the notes field line by line so that each line is entered as a separate record in another table creating a one-to-many relationship (this is the desired solution but once again I do not know how to reference the hard returns)

    Any ideas? Thanks.
    Last edited by kuau; 05-03-2010 at 03:23 AM. Reason: correction

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    $summary=str_replace("\r\n",'<br>',$summary);
    Last edited by james438; 05-03-2010 at 03:35 AM. Reason: added the br tag.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Or http://php.net/manual/en/function.nl2br.php

    Found this function from djr33
    Corrections to my coding/thoughts welcome.

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (05-03-2010)

  5. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    OMG, you have no idea what a huge dilemma this little function resolved. It worked first attempt and was so simple. Thanks a million! Thanks to Daniel too if you got it from him. I wish everything were this fast & simple.

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear James438:

    Thanks for your code. I was able to solve the problem with the nl2br function but wanted to try your method to see what it would do and all I got was a blank screen. What did I do wrong? I was curious to see if it could recognize the linefeeds even though there are no "\r\n" notations in the field.

    Code:
    <?php
    include_once('db-connectdb.php');
    $sql = "SELECT * FROM history WHERE client_id = '9' ";    
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
    $row = mysql_fetch_row($result);
    $note = $row['notes'];
    $note2 =str_replace("\r\n",'<br>',$note); 
    echo $note2;
    ?>
    Last edited by kuau; 05-03-2010 at 01:52 PM. Reason: added info

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Note: \n and \r are special characters that you can't see. They are encoded into the text as line breaks and depending on the system you will have one the other or both (linux, windows, mac). There's really no simple way around this. Using nl2br() seems the easy solution since as far as I've tested it works under all these conditions.
    If you want to do it manually, then you must replace three times:
    PHP Code:
    $var str_replace("\r\n",'<br>',$var); //for systems that send both as a group
    $var str_replace("\n",'<br>',$var); //for systems that only do \n
    $var str_replace("\r",'<br>',$var); //for system that only do \r 
    Technically \n is a new line and \r is a carriage return-- the difference? None, really, just that they are both used to encode a "line break". I think it is related to how the page lines up-- if you do a line break it does to a new line but a carriage return goes directly below rather than the other end? Basically once things became digital that was ignored.* Or something. Since I don't really understand the details, I just ignore it all and use nl2br(), though I see no reason that the code I posted above wouldn't work, but also no reason that it's better than nl2br() except that nl2br generates, as far as I've tested it, only <br /> for xhtml if that bothers you. You could also do nl2br() then search and replace <br /> to <br> as needed.



    (*I may be entirely wrong in my explanation here, but the background is something like that-- two different types of new lines that eventually served the same purpose. I've never seen the difference explained anywhere except that it's just a remnant of a different system in the past. And perhaps also yet another mac vs. pc operating system thing.)


    The only really important part of all this is that you cannot be sure the script is working just testing on your system. Someone else, for example on linux, may have entirely different results. Replacing \r\n will be just fine for windows (I think that's the one for windows...) but it will fail on other systems. Also, depending on how the text is stored it may be automatically converted from one to the other, so it's hard to guess.



    As for your code above I don't see anything wrong and the str_replace should be fine. Are you sure there's data output from the database? Try echoing that before the str_replace and that may show the problem is unrelated to the replacing.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Yep, nl2br really is the preferred way to do it. For some reason I just did not think of that when I posted earlier. I replace \r\n and it is on a linux based server.

    Using $var = str_replace("\r", '<br>'); will work for those systems that use \r to encode returns or \r\n, but not \n, which is why nl2br is the preferred method.
    To choose the lesser of two evils is still to choose evil. My personal site

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
  •