To ensure you remove all the extraneous white space you can do a quick loop.
[php]
While (strpos($StringVariable, " “) {
$StringVariable = str_replace(” “,” ",$StringVariable);
}
[/php]
What you have is the WHILE loop checks the string for an occourance of a DOUBLE SPACE. If it finds one it enters the loop and replaces ALL occurances of DOUBLE SPACEs with a SINGLE SPACE.
Problem… You have something that is a TRIPPLE SPACE. So now str_replace finds a single occurance of a DOUBLE SPACE and a single occurance of a SINGLE SPACE. It replaces the first 2 spaces (as a double space) with a single space. Then it moves on. No more occurances of a DOUBLE SPACE are found. But it leaves behind a SINGLE SPACE right next to a SINGLE SPACE (thus another Double Space).
That is why you need the loop.
I know it’s not clean cut but it indeed DOES work.
I tried it on my machine and I got exactly the results I expected…
[php]
<?
//With the WHILE loop
$StringVariable = "1 2 3 4";
While (strpos($StringVariable , " ")) {
$StringVariable = str_replace(" "," ",$StringVariable);
}
echo "
$StringVariable
";
// without the While loop
$StringVariable = "1 2 3 4";
$StringVariable = str_replace(" "," ",$StringVariable);
echo "
$StringVariable
";
?>
[/php]