title: Replace newline from MySQL TEXT field to parse w/ JSON
body: <p>"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.</p>
  
  <p>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...</p>
  
  <p><strong>But how do i replace newlinew from DB with \n?</strong></p>
  
  <p>I've tried this</p>
  
  <pre><code>$t = str_replace('&lt;br /&gt;', '\n', nl2br($t));
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>$t = preg_replace("/\r\n|\n\r|\r|\n/", "\n", $t);
  </code></pre>
  
  <p>and using CHAR(13) and CHAR(10), and still I get an error</p>
  
  <p>the new line in textarea is equivalent to, i guess</p>
  
  <pre><code>$t = 'text with a 
  newline';
  </code></pre>
  
  <p>it gives the same error. And in notepad i clearly see that it is <strong>crlf</strong></p>