preg_split与正则表达式给出错误的输出

Sha*_*que 0 php regex preg-split

我正在使用preg_split来表示字符串,但我没有获得所需的输出.例如

$string = 'Tachycardia limit_from:1900-01-01 limit_to:2027-08-29 numresults:10 sort:publication-date direction:descending facet-on-toc-section-id:Case Reports';
$vals = preg_split("/(\w*\d?):/", $string, NULL, PREG_SPLIT_DELIM_CAPTURE);
Run Code Online (Sandbox Code Playgroud)

正在产生输出

 Array
(
    [0] => Tachycardia 
    [1] => limit_from
    [2] => 1900-01-01 
    [3] => limit_to
    [4] => 2027-08-29 
    [5] => numresults
    [6] => 10 
    [7] => sort
    [8] => publication-date 
    [9] => direction
    [10] => descending facet-on-toc-section-
    [11] => id
    [12] => Case Reports
)
Run Code Online (Sandbox Code Playgroud)

哪个是错的,欲望输出它

Array
(
    [0] => Tachycardia 
    [1] => limit_from
    [2] => 1900-01-01 
    [3] => limit_to
    [4] => 2027-08-29 
    [5] => numresults
    [6] => 10 
    [7] => sort
    [8] => publication-date 
    [9] => direction
    [10] => descending
    [11] => facet-on-toc-section-id
    [12] => Case Reports
)
Run Code Online (Sandbox Code Playgroud)

正则表达式有问题,但我无法解决它.

小智 5

我会用

$vals = preg_split("/(\S+):/", $string, NULL, PREG_SPLIT_DELIM_CAPTURE);
Run Code Online (Sandbox Code Playgroud)

输出完全符合您的要求