Jump to content

[SOLVED] turning \r\n into <br />


Lodius2000

Recommended Posts

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

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.

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. :)

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.