Full width home advertisement

Post Page Advertisement [Top]

Remove multiple white space from string PHP Script


Today I'll introduce you with a PHP script that will remove any/many white space(s) from a string. Basically use trim(), ltrim() or rtrim() functions to remove the spaces. But that does not remove the multiple spaces from strings. So, the below function should help solve this issue.

<?php
     function replace_whitespace($Value = '') {
       // Replace any whitespace with only a single space
       return preg_replace('/\s+/', ' ', trim($Value));
     }
?>


How to use:

<?php
     echo '<pre>';
     $Text'M. B. Parvez        (Rony)';
     echo $Text,' - ',strlen($Text),'</br>';
     $Text = ReplaceWhitespace($Text);
     echo $Text,' - ',strlen($Text);
     echo '</pre>';
?>


Output:

M. B. Parvez      (Rony)- 23
M. B. Parvez (Rony)- 19


Bottom Ad [Post Page]