PHP在给定数组中的任何字符上爆炸


PHP explode on any characters in given array

有什么方法可以将字符串分隔符指定为数组中的任何字符吗?例如:

 $delimiter = range("a","z");
 $str = "103a765g678d6f76h";
 $newstr = $explode($delimiter, $str);

导致$newstr[103,765,678,6,76]

我在谷歌上找不到任何关于如何做到这一点的东西,我自己也想不出任何东西

您可以使用preg_split和正则表达式来实现您想要的。

只有当您必须首先在数组中range()您想要的字符时,implode()才是必需的,它只需将数组元素连接在一起即可形成字符串。

$delimiter = range("a","z");
$chars = implode($delimiter);
$str = "103a765g678d6f76h";
$newstr = preg_split("/[$chars]+/", $str, -1, PREG_SPLIT_NO_EMPTY);

演示

查看数据并使用preg_replace将字符范围替换为单个分隔符。然后分解修改后的字符串。

使用此

function multiexplode ($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}
 $delimiter = range("a","z");
     $str = "103a765g678d6f76h";
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$newstr = multiexplode($delimiter ,$str );
print_r($newstr);

从PHP爆炸页面来看,这应该非常适合您。。。不局限于字母。

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

分隔符必须是一个数组。

在PHP网站上阅读更多。。。

是使用:

preg_split()
delimiter = range("/a-z/");
 $str = "103a765g678d6f76h";
 $newstr =  preg_split($delimiter, $str);