由分隔符阵列爆炸

Joe*_*oeC 20 php explode

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

PHP手册:

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

而不是使用string $delimiter是否有任何方式使用array $delimiter而不会影响性能太多?

65F*_*f05 56

$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) );
Run Code Online (Sandbox Code Playgroud)


Ign*_*ams 29

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

  • 例如:`print_r(preg_split("/ [,.] /","0 1,2.3"));`将给你`数组([0] => 0 [1] => 1 [2] => 2 [3] => 3)`. (7认同)
  • `print_r(preg_split("/ [,\.] /","0 1,2.3"));"你的意思是:)谢谢,但我猜可能是最好的方式. (2认同)

Ale*_*Ale 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);
}
Run Code Online (Sandbox Code Playgroud)

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