Skip to content

Instantly share code, notes, and snippets.

@vuthaihoc
Created May 26, 2020 04:39
Show Gist options
  • Save vuthaihoc/62c5d1dce4bbe27eb1ffd8af2ed8f430 to your computer and use it in GitHub Desktop.
Save vuthaihoc/62c5d1dce4bbe27eb1ffd8af2ed8f430 to your computer and use it in GitHub Desktop.
<?php
/**
* $num can be 1 number, list or range
* Example
* $num = 1 => return [1]
* $num = "1,2,3,4" => return [1,2,3,4]
* $num = "1-3" => return [1,2,3]
*/
function numbersGenerator($num)
{
if (strpos($num, "-")) {
list($min, $max) = explode("-", $num);
for (; $min <= $max; $min++) {
yield (int)$min;
}
}else{
$nums = explode(",", $num);
foreach($nums as $n){
yield (int)$n;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment