Source Code:
(back to article)
<?php $string = " This is a string with excess whitespace. "; $string = trim($string); // Remove leading and trailing whitespace $string = str_replace(' ', ' ', $string); // Replace sequences of 2 spaces with 1 space while (strpos($string, ' ') !== false) { $string = str_replace(' ', ' ', $string); // Replace remaining sequences of 2 or more spaces with 1 space } echo $string; // Output: "This is a string with excess whitespace."
Result:
Report an issue