Stripping whitespace in PHP

Related

Based on this entry's title, content and categorisation, the following entries may be related:

Here’s a simple way to strip all whitespace from a string in PHP, which is useful for properly trimming any user input:

function realtrim($mixed) {
   if (is_array($mixed)) {
      return array_map('realtrim', $mixed);
   }
   if (strlen($mixed) > 0) {
      $mixed = trim(preg_replace('/[\s]+/', ' ', $mixed));
   }
   return $mixed;
}

The above function will strip all multiple spaces, tabs, line feeds and carriage returns. It also has the capability to accept an array as input, making it particularly suitable for web forms.

Comments are closed.

-->