1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Rep Power
    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?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Rep Power
    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.

    function trimspaces(&$string)
    $string = trim($string); //removes spaces at the front and back
    $prev=0; //sentinel: 0=not a space, 1=space
    $loop=0; //counter
    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) == " ")
  4. #3
  5. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    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]

Similar Threads

  1. Replies: 1
    Last Post: November 24th, 2003, 06:46 PM
  2. Stripping out spaces and new lines
    By whoopah in forum PHP Development
    Replies: 4
    Last Post: June 3rd, 2003, 06:31 PM
  3. Replies: 7
    Last Post: September 25th, 2002, 10:16 AM
  4. submit variables to fields with spaces in the name
    By lukemcd in forum PHP Development
    Replies: 3
    Last Post: September 10th, 2002, 10:21 PM
  5. stripping entities and spaces
    By Garrett_44 in forum PHP Development
    Replies: 8
    Last Post: April 27th, 2002, 08:19 AM

IMN logo majestic logo threadwatch logo seochat tools logo