PHP preg_split()

PHP preg_split()

我正在尝试使用 preg_split,如下所示:

$tarea = "13.3R4.2";
$keywords = preg_split("/[0-9]*.[0-9][a-zA-Z]/", $tarea);
print_r ($keywords);

我无法捕获数组 [0] 的值。下面是输出:

Array ( [0] => [1] => 4.2 ) 

我想捕获数组的两个索引。我不确定我在这里犯了什么错误。请帮忙!!

我期望的输出是:

Array ( [0] => 13.3R[1] => 4.2 ) 

谢谢

我认为您误解了 preg_split(). It splits a string on the specified separator, meaning it's not designed to return the separator. You want to be using preg_match() 到 return 匹配子字符串的目的:

$tarea = "13.3R4.2";
preg_match_all("/[0-9]+\.[0-9][a-z]?/i", $tarea, $matches);
print_r($matches);

下面的代码解决了这个问题:

$tarea = "13.3R4.2";
$keywords = preg_split('/(\d*\.\d\w?)/',$tarea, -1, PREG_SPLIT_DELIM_CAPTURE |     PREG_SPLIT_NO_EMPTY);
print_r ($keywords);

输出:

Array ( [0] => 13.3R [1] => 4.2 ) 

感谢大家的大力帮助!!!