php - Explode integer range with minuses
554
Is any better solution for get first and second value exploded by "-" with negative values than use explode function and check how many minuses are there?
$list = [
'10-20',
'12-14',
'-5-10',
'-10--2'
];
I would like to receive:
$first = 10; $second = 20;
$first = 12; $second = 14;
$first = -5; $second = 10;
$first = -10; $second = -2;
Answer
Solution:
You can use
to explode your string, searching for a
-
which is preceded by a digit:Output:
Demo on 3v4l.org