Here's a function to generate ranges from strings:
<?php
/* Creates an array of integers based on a given range string of format "int - int"
Eg. range_str('2 - 5'); */
function range_str($str) {
preg_match('#(\\d+)\\s*-\\s*(\\d+)#', $str, $matches);
if ( count($matches) == 3 ) {
return range($matches[1], $matches[2]);
}
return FALSE;
}
// Test
$array = range_str(' 2 - 4 ');
print_r($array);
?>
This outputs:
Array
(
[0] => 2
[1] => 3
[2] => 4
)