View Full Version : 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.
james438
05-03-2010, 03:29 AM
$summary=str_replace("\r\n",'<br>',$summary);
bluewalrus
05-03-2010, 03:59 AM
Or http://php.net/manual/en/function.nl2br.php
Found this function from djr33
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. :)
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.
<?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;
?>
djr33
05-03-2010, 05:08 PM
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:
$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.
james438
05-03-2010, 08:43 PM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.