August 24th, 2000, 06:55 PM
I have a string that contains words and spaces. I wan't to make sure there is only ONE space between each word. How can I do thes?
August 24th, 2000, 07:54 PM
excuse my indentation...
...this approach keeps the string as a string, thoughout ... turning it into an array, and converting back to a string at the end is more efficient, but the difference is unnoticable (unless the string is like 100,000 characters in length).
...if there's a mathmatical error, it's with the $loop variable somewhere... sorry, low on sleep & I didn't test this code.
$string = trim($string); //removes spaces at the front and back
$prev=0; //sentinel: 0=not a space, 1=space
while($loop++<strlen($string)) //loop for the length of the string
if($prev==1 && substr($string,$loop,$loop) == " ") //if the last char was a space
if(substr($string,$loop,$loop) == " ")
August 24th, 2000, 09:56 PM
or you could use a 1 line regular expression
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$var = preg_replace("/s+/", " ", $var);[/code]