简体   繁体   中英

Create specific patterns for a numeric range from given start and end numbers

I want to create a function which generates specific patterns to describe a numeric range between two given numbers. The function accepts two parameters: $startNumber and $endNumber . It should return an array of pattern strings. The best way to describe it is with examples:

myfunction(00000, 99999)     = array('*');
myfunction(10000, 29999)     = array('1*', '2*');
myfunction(68000, 68999)     = array('68*');
myfunction(0004000, 0008999) = array('0004*', '0005*', '0006*', '0007*', '0008*');
myfunction(5570000, 5659999) = array('557*', '558*', '559*', '560*', '561*', '562*', '563*', '564*', '565*');
myfunction(3760000, 5259999) = array('376*', '377*', '378*', '379*', '38*', '39*', '4*', '50*', '51*', '520*', '521*', '522*', '523*', '524*', '525*');
myfunction(12345, 45678)     = array('12345*', '12346*', '12347*', '12348*', '12349*', '1235*', '1236*', '1237*', '1238*', '1239*', '124*', '125*', '126*', '127*', '128*', '129*', '13*', '14*', '15*', '16*', '17*', '18*', '19*', '2*', '3*', '40*', '41*', '42*', '43*', '44*', '450*', '451*', '452*', '453*', '454*', '455*', '4560*', '4561*', '4562*', '4563*', '4564*', '4565*', '4566*', '45670*', '45671*', '45672*', '45673*', '45674*', '45675*', '45676*', '45677*', '45678*');
myfunction(000000, 399099)   = array('0*', '1*', '2*', '30*', '31*', '32*', '33*', '34*', '35*', '36*', '37*', '38*', '390*', '391*', '392*', '393*', '394*', '395*', '396*', '397*', '398*', '3990*');

Since some parts of your issue are not clear, I'll get closer to the first need you are claiming...

For the range

You don't need to recode the existing ;-)

Look for range() native function, can already do that for you:

function myfunction($start, $end)
{
    $r = range($start, $end);
    return array_map(function($n)
    {
        return $n.'*';
    }, $r);
}


var_dump(myfunction(34, 39));
// will output:

array(6) {
  [0]=>
  string(3) "34*"
  [1]=>
  string(3) "35*"
  [2]=>
  string(3) "36*"
  [3]=>
  string(3) "37*"
  [4]=>
  string(3) "38*"
  [5]=>
  string(3) "39*"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM