Explode and preg_split...........?

Discussion in 'PHP' started by strgraphics, Jul 6, 2010.

  1. #1
    i have 1000+ emails separated with commas, so i need to split them.., but i have one simple doubt...!!!

    we have explode and preg_split, which is best.., i believe both are same

    
    $res = explode(' , ', $emails)
    
    or
    
    $res = preg_split('/\s*,\s*/', $email);
    
    PHP:
    And may i know preg_split have ' \s*,\s* ' but explode have nothing...!!!
    Would be greatful to your help..!!!!
     
    strgraphics, Jul 6, 2010 IP
  2. TheDataPlanet.com

    TheDataPlanet.com Well-Known Member

    Messages:
    503
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    explode() is indefinitely better in this situation because it costs less. preg_split is a regular expression function which will spend extra server resources to parse the regexp string you have provided, namely '/\s*,\s*/'.
     
    TheDataPlanet.com, Jul 6, 2010 IP
  3. RobiMac

    RobiMac Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My guess would be explode, but before you commit one way or the other, you might want to think about taking a bit of time and try out both methods using timers, allowing you to make a reasoned decision based on performance metrics given your situation and not merely speculation.
     
    RobiMac, Jul 6, 2010 IP
  4. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    i didn't get any good reply...!!!!!!!!
     
    strgraphics, Jul 7, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    $res = explode(' , ', $emails)
    PHP:
    ^ Explode would match (space)(comma)(space)

    $res = preg_split('/\s*,\s*/', $emails);
    PHP:
    ^ Whereas the above preg_split() would match (new-line)(comma)(new-line)

    New-line as in any new-line/whitespace character (space, \n, \r\n, \t)


    So the best option out of the two in your scenario would be, are you wanting to match new-line(s) then go for the preg_replace(), or if your just wanting to match space(s) then go for the explode().
     
    danx10, Jul 8, 2010 IP
  6. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Ohh kk thanks friend...., thanks a lot.

    but one more doubt..., what \s*,\s* means in preg
     
    strgraphics, Jul 8, 2010 IP
  7. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    But danx,

    
    $string = 'one  ok  ,  two,three,     four  , five,six seven';
    $array = preg_split("/\s*,\s*/", $string); 
    print_r($array);
    PHP:
    and

    
    $string = 'one  ok  ,  two,three,     four  , five,six seven';
    $array = preg_split("/,/", $string); 
    print_r($array);
    
    PHP:
    Giving the same out put,

    Array ( [0] => one ok [1] => two [2] => three [3] => four [4] => five [5] => six seven )

    then what is the necessary of \s*,\s*, would be grateful about your help..!!
     
    strgraphics, Jul 8, 2010 IP