php用explode,可以提供多个字符作为分割符来进行分割数组吗?

explode — 使用一个字符串分割另一个字符串, 它的函数原型如下:

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

因此,它不可以提供多个字符作为分割符来进行分割数组。

 

如果要使用多个字符串作为分割字符,可以用另外一个函数 preg_split。

通过一个正则表达式分隔字符串, 它的函数原型如下:

array preg_split    ( string $pattern   , string $subject   [, int $limit = -1   [, int $flags = 0  ]] )

 

举例:

<?php
$str = "aa--bb++cc**dd";
$arr = preg_split('/[-+*]+/is', $str);
echo "<pre>";
print_r($arr);
echo "</pre>";
它的输出结果是:

Array
(
   [0] => aa
   [1] => bb
   [2] => cc
   [3] => dd
);
原文地址:https://www.cnblogs.com/archoncap/p/5228717.html