I have a string like this
4-1,4-1,,,1-1,,5-4,2-1,
and I need to extract the first number in each pair. For example, (4 -1, 4 -1,,, 1 - 1,, 5 - 4, 2 -1 ,)
Does anyone know how I could achieve this?
$string = '4-1,4-1,,,1-1,,5-4,2-1,';
preg_match_all('/(\d+)-/', $string, $matches);
// search for 1 or more digits that are followed by a hyphen, and return all matches
print_r($matches[1]);
Outputs:
Array
(
[0] => 4
[1] => 4
[2] => 1
[3] => 5
[4] => 2
)