Skip to content

Instantly share code, notes, and snippets.

@jrmadsen67
Created August 17, 2013 07:22
Show Gist options
  • Save jrmadsen67/6255706 to your computer and use it in GitHub Desktop.
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);
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