Created
August 17, 2013 07:22
-
-
Save jrmadsen67/6255706 to your computer and use it in GitHub Desktop.
Takes a string of numbers that may include , or - ranges & parses it into array Ex: chapters can be "1,3-6, 8-10" and will make an array of array(1,3,4,5,6,8,9,10);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function parse_list($chapters) | |
{ | |
//just a single number passed | |
if (is_numeric($chapters)) return array((int)$chapters); | |
$chapter_array = array(); | |
//break up based on , | |
$comma_sets = explode(',', $chapters); | |
//look at each & see if is range or no | |
foreach ($comma_sets as $key => $comma_set) { | |
if (strpos($comma_set, '-') === FALSE) | |
{ | |
$chapter_array[] = (int)$comma_set; | |
} | |
else | |
{ | |
list($first, $last) = explode('-', $comma_set); | |
$chapter_array = array_merge($chapter_array, range($first, $last)); | |
} | |
} | |
return $chapter_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment