Can we do multiple explode statements on one line in PHP? [duplicate]
Asked Answered
H

5

7

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.

Handrail answered 14/5, 2012 at 4:42 Comment(0)
D
17

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 
)
Deceit answered 14/5, 2012 at 4:47 Comment(3)
hi thanks for code but i have \r\n\ value and space to get explode its not working foreach(preg_split('/(\r\n\ s|&|,)/',$sms['sms_text']) as $no)Handrail
thankx bro solved foreach(preg_split("/\r\n|(\s|&|,)/",$sms['sms_text']) as $no)Handrail
/[\s&,]+/ would be more concise.Chiropody
R
4

Here is a great solution I found at PHP.net:

<?php

//$delimiters must be an array.

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

//And output will be like this:
// Array
// (
//    [0] => here is a sample
//    [1] =>  this text
//    [2] =>  and this will be exploded
//    [3] =>  this also 
//    [4] =>  this one too 
//    [5] => )
// )

?>
Ries answered 4/12, 2014 at 18:2 Comment(0)
S
2

you can use this

function multipleExplode($delimiters = array(), $string = ''){

    $mainDelim=$delimiters[count($delimiters)-1]; // dernier

    array_pop($delimiters);

    foreach($delimiters as $delimiter){

        $string= str_replace($delimiter, $mainDelim, $string);

    }

    $result= explode($mainDelim, $string);
    return $result;

} 
Sassy answered 14/5, 2012 at 4:46 Comment(0)
S
0

You could use preg_split() function to stplit a string using a regular expression, like so:

$text = preg_split('/( |,|&)/', $text);
Shantel answered 14/5, 2012 at 4:48 Comment(1)
Simplified pattern: /[ ,&]/. No capture group is needed and a characters class is more concise.Chiropody
C
0

I'd go with strtok(), eg

$delimiter = ' &,';
$token = strtok($sms['sms_text'], $delimiter);

while ($token !== false) {
    echo $token . "\n";
    $token = strtok($delimiter);
}
Cunha answered 14/5, 2012 at 4:52 Comment(1)
Of aaaaaaaaaaaall the duplicates for this task, this is the first time that someone actually included an implementation of strtok() instead of just plonking a link to the PHP docs. Good on ya. (...but the OP does ask for a one-liner)Chiropody

© 2022 - 2024 — McMap. All rights reserved.