SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do you replace multiple blank spaces with just 1 blank space

    Hello,

    Is there a Php Command or how do you replace multiple blank spaces in a string with just 1 blank space?
    For example say the string is:

    Code:
    $string = "Rental Scooter        Florida"
    as you can see there are multiple blank spaces between Scooter and Florida.
    So is there a function or code you can suggest that one would submit this string to and get:

    Code:
    $string = "Rental Scooter Florida"
    with ONLY 1 blank space between each Word of the string.

    Thanks.
    Last edited by cpradio; Sep 20, 2013 at 10:33.

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Cancelling down consecutive white spaces to just one within the string can be done with the following:
    PHP Code:
    //$str
    preg_replace('# {2,}#'' '$str); 
    If you'd like to specifically replace 2 or more white spaces with one in between words only, then the following should work:
    PHP Code:
    //$str
    preg_replace('#([a-z0-9\-]) {2,}([a-z0-9\-])#i''\1 \2'$str); 

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Neither of these codes is doing the Job!


    Quote Originally Posted by tpunt View Post
    Cancelling down consecutive white spaces to just one within the string can be done with the following:
    PHP Code:
    //$str
    preg_replace('# {2,}#'' '$str); 
    If you'd like to specifically replace 2 or more white spaces with one in between words only, then the following should work:
    PHP Code:
    //$str
    preg_replace('#([a-z0-9\-]) {2,}([a-z0-9\-])#i''\1 \2'$str); 

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    It works fine for me....

    PHP Code:
    $str 'First          Second';
    echo 
    "<pre>" $str "<br />"// Outputs "First          Second"
    echo preg_replace('# {2,}#'' '$str) . "</pre>"// Outputs "First Second" 
    Did you forget to put the echo statement in front of preg_replace?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi,

    Neither of these codes is doing the Job!
    The preg_replace() function does not pass its third argument by reference, so you will need to reassign the $str variable to the returned result of preg_replace():
    PHP Code:
    //$str
    $str preg_replace('# {2,}#'' '$str); 

  6. #6
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I am sorry, you are write, it works fine.
    Sorry again and thank you.

    Dean.


    Quote Originally Posted by cpradio View Post
    It works fine for me....
    PHP Code:
    $str 'First          Second';
    echo 
    "<pre>" $str "<br />"// Outputs "First          Second"
    echo preg_replace('# {2,}#'' '$str) . "</pre>"// Outputs "First Second" 
    Did you forget to put the echo statement in front of preg_replace?

    Anoox search engine volunteer

    www.anoox.com

  7. #7
    Non-Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try out this.

    You can use preg_replace to replace any sequence of whitespace chars with a dash...

    $string = preg_replace('/\s+/', '-', $string);

    Hope this helps.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •