SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot Goldfiles's Avatar
    Join Date
    Nov 2002
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP explode() white space issue

    I've got an issue with spacing in strings when I do explode(); I'm sure there is an easy fix for this

    The user enters a string into a form like this:
    one, two, three, four, five, six
    OR like this
    one,two,three,four,five,six
    sometimes they enter spaces after the commas, sometimes they don't. that is up to them, but it can screw up my explosion

    That string is saved as $keywords. I would like to explode $keywords so I can break it apart.

    If I do this:
    $exploded = explode(',', $keywords);
    then, that will a blank space in front of each keyword

    If I do this:
    $exploded = explode(', ', $keywords); //with a space after the comma
    then, that wont work for people that don't put spaces between their keywords

    Is there a better way to explode? or, if not, is there code that will take off the first character of a string if it is a blank white space?
    TextAdMarket - http://www.TextAdMarket.com
    Supply & Demand Advertising

  2. #2
    SitePoint Enthusiast cilverphox's Avatar
    Join Date
    Jan 2005
    Location
    Petoskey, MI
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use trim()

    You can use trim() after an "explosion" to eliminate the white space.

    Untested Code:

    PHP Code:
    $arrKeywords explosion(","$strKeywords)

    foreach(
    $arrKeywords as $index => $word)
       
    $arrKeywords[$index] = trim($word); 

    www.php.net is an amazing source for php help.

    -Cilverphox

  3. #3
    SitePoint Zealot Goldfiles's Avatar
    Join Date
    Nov 2002
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome, that worked great. thanks phox!
    TextAdMarket - http://www.TextAdMarket.com
    Supply & Demand Advertising

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing wrong with Cilverphox's answer but you can do it in one line if you like:
    PHP Code:
    $keywordsArray array_map('trim'explode(','$keywords)); 

  5. #5
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A better solution I think would be the following
    PHP Code:
    $keywords "one, two , three      , four";
    $keyword_array=explode(","trim($keywords));

    print_r($keyword_array); 

    //should just output 
    /* 
    Array 

        [0] => one 
        [1] => two 
        [2] => three 
        [3] => four 

    */ 
    This would avoid using the array map and stripping the white space on each of the exploded elements.
    Last edited by paul_wilkins; Aug 17, 2010 at 18:23.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    No Trim required...
    PHP Code:
    $s preg_split'/[, ]+/'$data, -1PREG_SPLIT_NO_EMPTY ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by bravoclicks View Post
    A better solution I think would be the following
    PHP Code:
    $keywords "one, two , three      , four";
    $keyword_array=explode(","trim($keywords)); 
    But that won't trim anything inside $keywords. It will only trim the left and the right of the full $keywords string.

    You'll have to use the solution given by cilverphox or markl999 instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •