Trim all whitespace from a string in PHP

To remove all extra whitespace from a string.

  • Remove leading and trailing whitespace
  • Remove double spaces and tabs
  • Convert internal white space to single space characters
$line = trim(preg_replace("'\s+'", ' ', $line));

Leave a Reply

Your email address will not be published. Required fields are marked *