Lodius2000 Posted June 27, 2008 Share Posted June 27, 2008 hi, I have a textarea, that inserts into mysql when i hit the enter key in the textarea it encodes as \r\n as we all know it does I want to get rid of the \r\n and turn it into a br so that when the contents of the textarea are displayed in a web page, the br's are there, here is my attempt and its results i put this into the text area this "is" a 'test' this is what went into the db this\r\n\"is\" \r\na\r\n\r\n\'test\' this is what displays on the web page with stripslashes() used thisrn"is" rnarnrn'test' and this is the code that creates the db insertion variable from the contents of the textarea <?php $article = mysql_real_escape_string($_POST['body']); //replace new lines with <br /> $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $new_article = str_replace($order, $replace, $article); //$new_article gets inserted as part of a new row ?> i tried nl2br also and it produced almost the same, the <br />'s were there in the web page but the r's and n's were too what am I doing wrong Thanks Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/ Share on other sites More sharing options...
ratcateme Posted June 27, 2008 Share Posted June 27, 2008 try reording your code so the string is escaped after you fix the line ends like this: //replace new lines with <br /> $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $new_article = str_replace($order, $replace, $article); $article = mysql_real_escape_string($_POST['body']); Scott. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575685 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 I usually use preg_replace() for sanitation. You might want to look at that also. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575709 Share on other sites More sharing options...
trq Posted June 27, 2008 Share Posted June 27, 2008 I usually use preg_replace() for sanitation. You might want to look at that also. preg_replace should only be used for pattern matching. If you have explicit strings/chars to match it is mutch slower to use preg_match for no reason. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575774 Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 I think what you may be looking for is nl2br(); This function turns new lines (\n or \n\r) into HTML line breaks (<br />) for you. I find it a very useful function. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575777 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 yeah... preg_replace is an overkill if the string itself is explicit... also for this case, nl2br() wont work... nl2br() can only deal with \n and not \r. for this case, you may use str_replace(). Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575783 Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 It shouldn't make a difference, \n\r is just a newline for windows machines. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575785 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 not to all... *nix on the other hand only have \n. I had been into this trouble also, I have tried using nl2br() and things were not working. From personal experience, I replaced those strings with <br> or you may also strip \r then use nl2br() to display it properly. Well, this is based on my experience that is. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575790 Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 I know *nix uses \n, hence why I specifically said Windows machines. But based on this I've never had a problem. On both kinds of servers, nl2br() has always worked perfectly fine for me. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575792 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 well, this seems to be a talk of personal experiences. anyway, Lodius2000, its up to you to choose which one would work for you. Its just that I had been in that trouble before also where nl2br() didn't work. cheers, Jay Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575795 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 This is what you're looking for. http://php.net/nl2br Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575798 Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 Xyn: If you've been reading, that is what we've been discussing. bluejay: I suppose it is, I guess in any languages people will have their own experiences, but Lodius2000 there's no reason why you shouldn't at least try nl2br(), it may or may not work and if it does it's just saved you a lot of time and effort. Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575800 Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 Wolphine, i did notice, except maybe if i showed the nl2br php page, agreeing with everyone else, perhaps the function will be used, and the thread will be solved (I didn't intend anything funny with my post.)? Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-575808 Share on other sites More sharing options...
Lodius2000 Posted June 27, 2008 Author Share Posted June 27, 2008 so are the \r\n's in there because of mysql_real_escape_string or are they in there because that is the place-holder html encodes when you hit enter in a textarea because I tried ratcateme's advice and code now reads //replace new lines with <br /> $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // Processes \r\n's first so they aren't converted twice. $new_article = str_replace($order, $replace, $article); $article_final = mysql_real_escape_string($new_article); //$article_final gets inserted and it inserted nothing at all, with the same string as above in my original post also, again nl2br does not work, ive put it in every location on both the insertion page and webpage that I can think of and it adds <br />s but the r's and n's are still there too Link to comment https://forums.phpfreaks.com/topic/112132-solved-turning-rn-into/#findComment-576376 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.