PHP preg_split()用于换行逗号冒号和空格

时间:2016-02-25 18:14:17

标签: php preg-split

这是我在.php中的代码:

$new_split = preg_split("/\s*[:, ]\s*/",$full_list,2);
print_r ($new_split);

输入($ full_list)是:

abcd : xyz
abcd efgh, ijk ,lmn
abcd lmnop
abcd: efghijk
abcd,efgh

输出是:

Array ( 
    [0] => abcd 
    [1] => xyz abcd efgh, ijk ,lmn abcd lmnop abcd: efghijk abcd,efgh *
)

我想根据新行逗号(,)冒号(:)和空格进行拆分。请让我知道如何获得以下输出。

预期输出为:

Array ( 
    [0] => abcd 
    [1] => xyz 
    [2] => abcd 
    [3] => efgh 
    [4] => ijk 
    [5] => lmn 
    [6] => abcd 
    [7] => lmnop 
    [8] => abcd 
    [9] => efghijk 
    [10] => abcd 
    [11] =>efgh 
)

2 个答案:

答案 0 :(得分:2)

删除字符类周围的\s*并在字符类中更改\s的单个空格,同时添加一个量词(即。+为1或更多):

$new_split = preg_split("/[:,\s]+/",$full_list,2);
print_r ($new_split);

答案 1 :(得分:1)

在括号内添加\s,如下所示:$new_split = preg_split("/\s*[:,\s]\s*/",$full_list);