title: Get same result for php `str_len ` as for jQuery `.val().length()`
body: <p>I use jQuery to count the value of a textarea on the fly:</p>
  
  <pre><code>function count_chars () {
      count_chars=$('#text_textarea').val().length;
  }
  </code></pre>
  
  <p>...then on submit serialize the form, send the text of the textarea via ajax to a php file which then validates the text on the server side. However, I got problems with newlines and spaces.</p>
  
  <p>Of course, if I just get the text "as it is" from the textarea, php will count each new line as two or 4 characters (<code>\n</code>, ...). So I tried to replace them with something like this:</p>
  
  <pre><code>strlen(str_replace(array("\r", "\n"), ' ', $text)))
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>strlen(preg_replace('/\s+/', ' ', trim($text)))
  </code></pre>
  
  <p>However, if I got e.g. 10 paragraphs and jQuery returns 2500 characters, php will either return 2510 or 2490, depending if I replace new lines with a space or remove them completely. So the difference is 20, but there are only 10 new lines...?</p>
  
  <p>What am I missing? How can I get php to return the same result as jQuery? Where is the problem, in php or in jQuery?</p>