I'm trying to get all substrings matched with a multiplier:
$list = '1,2,3,4';
preg_match_all('|\d+(,\d+)*|', $list, $matches);
print_r($matches);
This example returns, as expected, the last match in [1]
:
Array
(
[0] => Array
(
[0] => 1,2,3,4
)
[1] => Array
(
[0] => ,4
)
)
However, I would like to get all strings matched by (,\d+)
, to get something like:
Array
(
[0] => ,2
[1] => ,3
[2] => ,4
)
Is there a way to do this with a single function such as preg_match_all()
?
,
. – Riebling[0] => ,2
is not possible with PHP. is,2
a string or is it a number? – Algolexplode(...)
is the better option here. You could do:preg_match_all('|(\d+)|', $list, $matches);
, but there is no guarantee the input string is a comma delimited string with numbers! – Thespian