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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.