How in a string
-1 -2 -3 -4
get
array(-1, -2, -3, -4)
using regex and function
preg_match_all
Any help is appreciated! Thanks!
How in a string
-1 -2 -3 -4
get
array(-1, -2, -3, -4)
using regex and function
preg_match_all
Any help is appreciated! Thanks!
Use this:
/(([-]?\d+))/gmi
The result:
MATCH 1
1. [1-3] `-1`
MATCH 2
1. [8-10] `-2`
MATCH 3
1. [15-17] `-3`
MATCH 4
1. [22-24] `-4`
See demo
In PHP usage:
$string = '(-1) * (-2) - (-3) * (-4)';
$regex = '/(([-]?\d+))/i';
preg_match_all($regex, $string, $matches);
print_r($matches[1]);
Result:
Array ( [0] => -1 [1] => -2 [2] => -3 [3] => -4 )