eliminate space from a field

Discussion in 'PHP' started by kichus, Dec 7, 2007.

  1. #1
    Hi All

    I have a field which contain data with spaces .these spaces are long and are mostly in between words.

    How can I eliminate these spaces and get data in a sentence with only single spaces.


    Thanks
     
    kichus, Dec 7, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $text = preg_replace('~\s{2,}~', ' ', $text);
    
    PHP:
     
    nico_swd, Dec 7, 2007 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    Not sure if you want spaces at all, so yeah.. This gets rid of all spaces.
    
    $spaces = array(" ", " ");
    $text = "Hello  World      Hello     Again ";
    $text = str_replace($spaces, "", $text);
    
    Code (php):
     
    Kaizoku, Dec 7, 2007 IP
  4. kichus

    kichus Peon

    Messages:
    188
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi

    thank you both for your help.
    $text = preg_replace('~\s{2,}~', ' ', $text); --worked perfect.

    one more question... i have a string i want to extract a particular value from it (location).there are too many similar symbols in the string so i am not able to do it in the usual way.

    But if taken from the back i can easily extract the location value.. but don't have any idea how to do it...
    ---------------------------------
    this is the string:front street Unique historical building Office Space - 1925 square foot + Call 416-485-9323 (18 Nov 2007) (416) 4859323, - Toronto, ON Mount Pleasant West 3

    I want to extract "Toronto, ON Mount Pleasant West 3" from it


    ------------------------
    is there an easy code for it
     
    kichus, Dec 13, 2007 IP
  5. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #5
    a very simple funtion trim() which will remove all unnessery spaces from string

    ex: $str=trim($srt);
     
    sunnyverma1984, Dec 14, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6

    
    $location = trim(end(explode('-', $string)));
    
    PHP:
    trim() does only remove spaces from the beginning and end of the string, and not in between.
     
    nico_swd, Dec 14, 2007 IP