Harinder Harinder - 6 months ago 65
PHP Question

Can we do multiple explode statements on one line in PHP?

Can we do multiple explode() in PHP?

For example, to do this:

foreach(explode(" ",$sms['sms_text']) as $no)
foreach(explode("&",$sms['sms_text']) as $no)
foreach(explode(",",$sms['sms_text']) as $no)

All in one explode like this:

foreach(explode('','&',',',$sms['sms_text']) as $no)

What's the best way to do this? What I want is to split the string on multiple delimiters in one line.


If you're looking to split the string with multiple delimiters, perhaps preg_split would be appropriate.

$parts = preg_split( '/(\s|&|,)/', 'This and&this and,this' );
print_r( $parts );

Which results in:

Array ( 
  [0] => This 
  [1] => and 
  [2] => this 
  [3] => and 
  [4] => this