Harinder Harinder - 1 year ago 137
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.

Answer Source

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 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download