Replace newline from MySQL TEXT field to parse w/ JSON

Posted by dr3w on Stack Overflow See other posts from Stack Overflow or by dr3w
Published on 2010-03-31T07:20:11Z Indexed on 2010/03/31 7:23 UTC
Read the original article Hit count: 380

Filed under:
|
|

Hi, "replace newline" seems to be a question asked here and there like hundred times already. But however, i haven't found any working solution for myself yet.

I have a textarea that i use to save data into DB. Then using AJAX I want to get data from the DB in the backend that is in TEXT field and to pass it to frontend using JSON. But pasing JSON returns an error, as new lines from DB are not valid JSON syntax, I guess i should use \n instead...

But how do i replace newlinew from DB with \n?

I've tried this

$t = str_replace('<br />', '\n', nl2br($t));

and this

$t = preg_replace("/\r\n|\n\r|\r|\n/", "\n", $t);

and using CHAR(13) and CHAR(10), and still I get an error

the new line in textarea is equivalent to, i guess $t = 'text with a newline'; it gives the same error. And in notepad i clearly see that it is crlf

© Stack Overflow or respective owner

Related posts about php

Related posts about newline