preg_match_all 以逗号分隔,值可能包含空格

preg_match_all separated by commas with values probably containing spaces

我目前有一个 preg_match_all 用于不包含 space 的常规字符串,但我现在需要使其适用于每个 space.[=18= 之间的任何内容]

我需要 abc, hh, hey there, 1 2 3, hey_there_ 到 return abc hh hey there``1 2 3 hey_there_

但是当涉及 space 时,我当前的脚本就停止了。

preg_match_all("/([a-zA-Z0-9_-]+)+[,]/",$threadpolloptions,$polloptions);
foreach(array_unique($polloptions[1]) as $option) {
     $test .= $option.' > ';
}

在这种情况下您不需要正则表达式。爆炸会更快

$str = 'abc, hh, hey there, 1 2 3, hey_there_';
print_r(explode(', ', $str));

结果

Array
(
    [0] => abc
    [1] => hh
    [2] => hey there
    [3] => 1 2 3
    [4] => hey_there_
)

更新

$str = 'abc, hh,hey there, 1 2 3, hey_there_';
print_r(preg_split("/,\s*/", $str));

结果相同

你可以使用 explode():

$string = "abc, hh, hey there, 1 2 3, hey_there_";
$array = explode(',', $string);

foreach($array as $row){
    echo trim($row, ' ');
}

您可以将 explodearray_map 一起用作

$str = 'abc, hh, hey there, 1 2 3, hey_there_';
var_dump(array_map('trim',explode(',',$str)));

Fiddle