21

区切り文字の配列を使用してexplode()を実行する方法はありますか?

PHPマニュアル:

配列の分解(文字列$ delimiter、文字列$ string [、int $ limit])

使用する代わりに、パフォーマンスにあまり影響を与えずstring $delimiterに使用する方法はありますか?array $delimiter

4

5 に答える 5

58
$str = 'Monsters are SUPER scary, bro!';
$del = array('a', 'b', 'c');

// In one fell swoop...
$arr = explode( $del[0], str_replace($del, $del[0], $str) );
于 2010-12-06T20:36:08.160 に答える
31

Use preg_split() with an appropriate regex.

于 2010-05-18T19:07:06.660 に答える
6
function explode_by_array($delim, $input) {
  $unidelim = $delim[0];
  $step_01 = str_replace($delim, $unidelim, $input); //Extra step to create a uniform value
  return explode($unidelim, $step_01);
}

That's improved @65Fbef05's code. We use first delimiter, because "+delim+" may be used in original string.

于 2012-07-07T19:36:07.937 に答える
0

The above suggestions won't work if the delimiters after the first delimiter include characters from that first delimiter. For instance, if you want to use line breaks as delimiters, but you're not sure if your input uses \r\n, \r or just \n, you can't use the above methods.

$str = '___RN___RN___R___N___RN___RN';
$del = array('RN', 'R', 'N');

# This won't work if delimiters 2, 3, n include characters from delimiter 1
var_dump(explode( $del[0], str_replace($del, $del[0], $str)));

This will output:

array(11) {
  [0]=>
  string(4) "___R"
  [1]=>
  string(0) ""
  [2]=>
  string(4) "___R"
  [3]=>
  string(0) ""
  [4]=>
  string(4) "___R"
  [5]=>
  string(3) "___"
  [6]=>
  string(4) "___R"
  [7]=>
  string(0) ""
  [8]=>
  string(4) "___R"
  [9]=>
  string(0) ""
  [10]=>
  string(0) ""
}

Which isn't ideal if you're planning to do string comparisons. Instead, you'll need to get a bit more complex. What I have written below may not be the most efficient and succinct, but it does the trick.

# This, however, will work
function array_explode($delimiters, $string){
    if(!is_array(($delimiters)) && !is_array($string)){
        //if neither the delimiter nor the string are arrays
        return explode($delimiters,$string);
    } else if(!is_array($delimiters) && is_array($string)) {
        //if the delimiter is not an array but the string is
        foreach($string as $item){
            foreach(explode($delimiters, $item) as $sub_item){
                $items[] = $sub_item;
            }
        }
        return $items;
    } else if(is_array($delimiters) && !is_array($string)) {
        //if the delimiter is an array but the string is not
        $string_array[] = $string;
        foreach($delimiters as $delimiter){
            $string_array = array_explode($delimiter, $string_array);
        }
        return $string_array;
    }
}

var_dump(array_explode($del,$str));

It will output the following:

array(7) {
  [0]=>
  string(3) "___"
  [1]=>
  string(3) "___"
  [2]=>
  string(3) "___"
  [3]=>
  string(3) "___"
  [4]=>
  string(3) "___"
  [5]=>
  string(3) "___"
  [6]=>
  string(0) ""
}

Have a play: https://3v4l.org/bJOkI

于 2018-08-08T11:49:57.133 に答える
-2

php's explode method doesn't support multiple delimiters, so you can't pass it an array. Also, what kind of string are you parsing that has multiple delimiters? you're best bet would be to loop through your delimiters, and re-explode some of the exploded strings.

于 2010-05-18T19:08:20.287 に答える