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));
}
?>
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>';
?>
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
M. B. Parvez (Rony)- 19