PHP 8.5.0 Alpha 1 available for testing

Voting

: five minus five?
(Example: nine)

The Note You're Voting On

Aram Kocharyan
14 years ago
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
)

<< Back to user notes page

To Top