Skip to content

Instantly share code, notes, and snippets.

@xus
Created May 23, 2017 08:52
Show Gist options
  • Save xus/7cf5d016b033dbb48dbb25aac3038e7f to your computer and use it in GitHub Desktop.
Save xus/7cf5d016b033dbb48dbb25aac3038e7f to your computer and use it in GitHub Desktop.
Best way to remove whitespaces and linebreaks in PHP
// Sometimes the whitespaces contains bullshit and we need to clean this mess.
// I saw the light here --> http://heavydots.com/blog/when-the-white-space-became-a-beast
// Same with breaklines.
$ascii_whitespaces = chr(194).chr(160);
$ascii_breaklines = chr(13).chr(10);
$to_replace = array ($ascii_whitespaces, $ascii_breaklines);
$replace_to = array (' ', '');
$text = $_POST['text']; // YOUR TEXT HERE...
//count characters
$chars_entered = strlen(strip_tags(str_replace($to_replace, $replace_to, $text)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment