replace multiple spaces with a single space in a string in php
by rajesh[ Edit ] 2010-01-03 14:40:57
We will use ereg_replace to replace multispace (multiple spaces) in a string with one single space
function multiple_space_replace($string)
{
// using ereg_replace to replace space, tabs and newline in a given string
$str = ereg_replace("[ \t\n\r]+", " ", $string);
return $str;
}
$string = "This is testing multi space replace";
$newstr = multiple_space_replace($string);
echo $newstr;
?>
Result:This is testing multi space replace