PHP code example of dsentker / numeric-range-parser

1. Go to this page and download the library: Download dsentker/numeric-range-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dsentker / numeric-range-parser example snippets


$parser = new DefaultNumericRangeParser();
$result = $parser->parse('1-3;5');
dump($result->toNormalizedArray()); // [1, 2, 3, 5]


// $result is traversable, iterating over indexes is possible too
foreach ($result as $index) {
    echo $index . PHP_EOL;
}


$parser = new DefaultNumericRangeParser();
$result = $parser->parse('4-6; 10'); // two blocks defined here

$result->append(new \ArrayIterator([1-2])); // another block here

foreach ($result as $index) {
    // $result->getIteratorIndex() will count up to 2 (0,1,2)
    printf("Block #%d: Index: #%d", $result->getIteratorIndex(), $result->current());
}

$instance = new \DSentker\DefaultNumericRangeParser('..', '/');
$result = $instance->parse('1..4 / 6..10');

$default = new \DSentker\DefaultNumericRangeParser();
$strict = new \DSentker\StrictNumericRangeParser();

$default->parse('10-8'); // No error
$strict->parse('10-8'); // First index is greater than second, RangeException is thrown

$default->parse('8-10;;11'); // No error
$strict->parse('8-10;;11'); // Missing range, RangeException is thrown

$default->parse('8a;10;12'); // No error
$strict->parse('8a;10;12'); // Invalid character, RangeException is thrown