title: Cannot replace carriage return and new line in php
body: <p>I get a string from the frontend which has a line break in it. It is saved in an array which looks like this:</p>
  
  <pre><code>[address] =&gt; Array (
      [0] =&gt; Foo
  Bar
  )
  </code></pre>
  
  <p>I then use json_encode() on the array before writing it into the SQL DB: </p>
  
  <pre><code>$string = json_encode( $string, JSON_UNESCAPED_UNICODE );
  </code></pre>
  
  <p>This turns the array into:</p>
  
  <pre><code>{"address":["Foo\r\nBar"]}
  </code></pre>
  
  <p>Unfortunately the DB doesn't like \r or \n if not escaped, so it gets rid of the \r and \n. </p>
  
  <ol>
  <li>So the first question is, is there a function that I can use to properly escape the string, so it can be written properly into the DB without losing the line break?</li>
  </ol>
  
  <hr>
  
  <ol start="2">
  <li>I didn't find any function for that, so I tried to use str_replace to just replace the \r\n with \\n. The function is:</li>
  </ol>
  
  <p><code>$string = str_replace(["\r\n","\r","\n"], "\\n", $string);</code></p>
  
  <p>This however does not work. I don't know why. The function itself works, as I tried to replace only "n" with "bla" and it worked. However the moment I try to replace the backslash it does not find anything to replace. I don't know if some "special" backslash character is used here or what else could be going on here.</p>
  
  <p>This is driving me nuts, seriously. So I hope somebody can help me out. Thanks in advance.</p>