bradleyboy / range-converter
Simple class that creates compact string representations of integer arrays. Can also convert those strings back to an array.
0.1.0
2014-11-20 20:11 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: @stable
This package is not auto-updated.
Last update: 2024-12-03 03:38:11 UTC
README
This simple class reduces arrays into compact strings by creating ranges when encountering neighboring integers. It can also expand strings with ranges back to the original array form.
Examples
Reducing ranges:
$converter = new Bradleyboy\Util\RangeConverter; $converter->reduce([1,2,3,4,7,9,10,11]); // Returns: '1..4,7,9..11'
Expanding ranges:
$converter = new Bradleyboy\Util\RangeConverter; $converter->expand('1..4,7,9..11'); // Returns: [1,2,3,4,7,9,10,11]
You can also set custom separators:
$converter = new Bradleyboy\Util\RangeConverter; $converter->setSeparator('.') ->setRangeSeparator('-') ->reduce([1,2,3,4,7,9,10,11]); // Returns: '1-4.7.9-11'
For more examples, see src/Bradleyboy/Util/RangeConverterTest.php
.