按分隔符数组进行爆炸

发布于 2024-09-02 04:40:44 字数 212 浏览 7 评论 0原文

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

PHP 手册:

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

有没有办法不使用 string $delimiter 而使用 array $delimiter太影响性能了?

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

PHP Manual:

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

Instead of using string $delimiter is there any way to use array $delimiter without affecting performance too much?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青朷 2024-09-09 04:40:44
$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) );
$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) );
忘年祭陌 2024-09-09 04:40:44

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

Use preg_split() with an appropriate regex.

北陌 2024-09-09 04:40:44
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);
}

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

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.

黯然 2024-09-09 04:40:44

如果第一个分隔符之后的分隔符包含该第一个分隔符中的字符,则上述建议将不起作用。例如,如果您想使用换行符作为分隔符,但不确定输入是否使用 \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)));

这将输出:

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) ""
}

如果您打算进行字符串比较,这并不理想。相反,您需要变得更复杂一些。我下面写的可能不是最有效和最简洁的,但它确实有效。

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

它将输出以下内容:

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

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

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

度的依靠╰つ 2024-09-09 04:40:44

接受了@65Fbef05的答案,把它变成了一个函数。注意:这与原始爆炸函数一样区分大小写。

public static function explodeByArray(array $delimeters, string $input): array {
    if($delimeters===[]){
        return [$input];
    }
    $unidelim = $delimeters[0];
    $step     = str_replace($delimeters, $unidelim, $input);
    return explode($unidelim, $step);
}

并进行了单元测试(PHPUnit),因为我总是对 stackoverflow 上的答案有点怀疑:

/**
 * @dataProvider dataProviderForExplodeByArray
 * @param array  $delimeters
 * @param array  $expected
 * @param string $subject
 * @return void
 */
public function testExplodeByArray(array $delimeters, array $expected, string $subject='The quick brown fox jumps over the lazy dog'): void {
    self::assertSame($expected, explodeByArray($delimeters, $subject));
}

public function dataProviderForExplodeByArray(): array{
    return [
        [[], ['The quick brown fox jumps over the lazy dog']],
        [['zzz'], ['The quick brown fox jumps over the lazy dog']],
        [['the'], ['The quick brown fox jumps over ', ' lazy dog']],
        [['The quick brown fox jumps over the lazy dog'], ['', '']],
        [['The'], ['', ' quick brown fox jumps over the lazy dog']], 
        [['dog'], ['The quick brown fox jumps over the lazy ', '']], 
        [['the', 'quick'], ['The ', ' brown fox jumps over ', ' lazy dog']],
        [['quick', 'the'], ['The ', ' brown fox jumps over ', ' lazy dog']],
        [['quick', 'the', 'fox'], ['The ', ' brown ', ' jumps over ', ' lazy dog']],
        [['over', 'fox'], ['The ', 'y brown ', ' jumps ', ' the lazy ', ''], 'The foxy brown fox jumps over the lazy fox'],
    ];
}

Took @65Fbef05's answer, turned it into a func. Note: this is case sensitive like the original explode function.

public static function explodeByArray(array $delimeters, string $input): array {
    if($delimeters===[]){
        return [$input];
    }
    $unidelim = $delimeters[0];
    $step     = str_replace($delimeters, $unidelim, $input);
    return explode($unidelim, $step);
}

And unit tested (PHPUnit) as I'm always a bit suspicious of answers on stackoverflow:

/**
 * @dataProvider dataProviderForExplodeByArray
 * @param array  $delimeters
 * @param array  $expected
 * @param string $subject
 * @return void
 */
public function testExplodeByArray(array $delimeters, array $expected, string $subject='The quick brown fox jumps over the lazy dog'): void {
    self::assertSame($expected, explodeByArray($delimeters, $subject));
}

public function dataProviderForExplodeByArray(): array{
    return [
        [[], ['The quick brown fox jumps over the lazy dog']],
        [['zzz'], ['The quick brown fox jumps over the lazy dog']],
        [['the'], ['The quick brown fox jumps over ', ' lazy dog']],
        [['The quick brown fox jumps over the lazy dog'], ['', '']],
        [['The'], ['', ' quick brown fox jumps over the lazy dog']], 
        [['dog'], ['The quick brown fox jumps over the lazy ', '']], 
        [['the', 'quick'], ['The ', ' brown fox jumps over ', ' lazy dog']],
        [['quick', 'the'], ['The ', ' brown fox jumps over ', ' lazy dog']],
        [['quick', 'the', 'fox'], ['The ', ' brown ', ' jumps over ', ' lazy dog']],
        [['over', 'fox'], ['The ', 'y brown ', ' jumps ', ' the lazy ', ''], 'The foxy brown fox jumps over the lazy fox'],
    ];
}
﹉夏雨初晴づ 2024-09-09 04:40:44

php的explode方法不支持多个分隔符,所以你不能向它传递一个数组。
另外,您正在解析什么样的具有多个分隔符的字符串?最好的办法是循环遍历定界符,并重新分解一些分解的字符串。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文