php 按定界符数组展开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2860238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:56:44  来源:igfitidea点击:

Exploding by Array of Delimiters

phpexplode

提问by JoeC

Is there any way to explode() using an array of delimiters?

有没有办法使用分隔符数组来爆炸()?

PHP Manual:

array explode ( string $delimiter , string $string [, int $limit ] )

PHP手册:

数组爆炸(字符串 $delimiter ,字符串 $string [, int $limit ] )

Instead of using string $delimiteris there any way to use array $delimiterwithout affecting performance too much?

而不是使用string $delimiter有什么方法可以在array $delimiter不影响性能的情况下使用?

回答by 65Fbef05

$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) );

回答by Ignacio Vazquez-Abrams

Use preg_split()with an appropriate regex.

使用preg_split()与适当的正则表达式。

回答by Ale

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.

这改进了@65Fbef05 的代码。我们使用第一个分隔符,因为"+delim+"可能在原始字符串中使用。

回答by dearsina

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.

如果第一个分隔符之后的分隔符包含来自第一个分隔符的字符,则上述建议将不起作用。例如,如果您想使用换行符作为分隔符,但您不确定您的输入是使用 \r\n、\r 还是仅使用 \n,则不能使用上述方法。

$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

玩一玩:https: //3v4l.org/bJOkI

回答by GSto

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.

php的explode方法不支持多个分隔符,所以不能传递数组。另外,您正在解析哪种字符串具有多个分隔符?你最好的选择是遍历你的分隔符,并重新分解一些分解的字符串。