replace multiple spaces with a single space in a string in php

by rajesh 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

Tagged in:

3147
like
0
dislike
0
mail
flag

You must LOGIN to add comments