Skip to content

Instantly share code, notes, and snippets.

@trebi
Last active August 29, 2015 13:56
Show Gist options
  • Save trebi/9197673 to your computer and use it in GitHub Desktop.
Save trebi/9197673 to your computer and use it in GitHub Desktop.
Expand and sort print page ranges
$in = "498,500-504,540,542-546,592,594-598,650-654,658,698,700-704,758,810,54-70";
$r = explode(",", $in );
$r2 = $r;
foreach ($r2 as $k => $v) {
if (strpos($v, "-") !== false) {
//var_dump($v);
$ab = explode("-", $v);
$r[$k] = $ab[0];
for ($i = $ab[0]+1; $i <= $ab[1]; $i++) {
$r[] = $i;
}
}
}
asort($r);
$r = array_unique($r);
echo implode(",", $r); //54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,498,500,501,502,503,504,540,542,543,544,545,546,592,594,595,596,597,598,650,651,652,653,654,658,698,700,701,702,703,704,758,810
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment