简体   繁体   English

生成两个字母数字值之间的范围

[英]Generate a range between two alphanumeric values

I have a set of data where each item has a 'range from' and 'range to' string of varying types. 我有一组数据,其中每个项目都有一个“范围从”和“范围到”各种类型的字符串。 For example the first item might have 3001A -> 4000A and the next item might be DE25500 -> DE27419 and so on (with several different patterns but usually consisting of a static section and a range section (it seems that usually any letter is static and numbers can either be static or not). 例如,第一项可能具有3001A-> 4000A,下一项可能是DE25500-> DE27419,依此类推(具有几种不同的模式,但通常由一个静态部分和一个范围部分组成(似乎通常任何字母都是静态的,而数字可以是静态的,也可以不是。

Is there any ready built function in PHP that can cope with generating the intermediate values in the range? PHP中是否有任何现成的内置函数可以应付范围内的中间值? Or if not any tips on how to build one? 或者如果没有关于如何构建一个的任何提示?

I'm no PHP expert but couldnt you use a for loop for that. 我不是PHP专家,但您不能为此使用for循环。

You will need to extract the parts of your values that represent the ranges and then start the loop from the lowest to the highest. 您将需要提取代表范围的值的各个部分,然后从最低到最高开始循环。

$min = (int) substr("DE25500", 2)
$max = (int) substr("DE27419", 2)
for ($x = $min; $x <= $max; $x++) {
    // logic in here
}

I'd suggest you to first find all the patterns and then write functions that knows how to subdivide the numeric part from the litteral one. 我建议您首先找到所有模式,然后编写函数,该函数知道如何从乱码中细分数字部分。 Then to work with ranges in PHP you can use http://php.net/range 然后,要在PHP中使用范围,可以使用http://php.net/range

Figured out a function to do this in the end... 最终想出一个函数来执行此操作...

function generateRange($startString, $endString, $step = 1)
{
    if (strlen($startString) !== strlen($endString)) {
        throw new LogicException('Strings must be equal in length');
    }

    // Get position of first character difference
    $position = mb_strlen(mb_strcut($startString, 0, strspn($startString ^ $endString, "\0")));
    if (!is_numeric($startString[$position]) || !is_numeric($endString[$position])) {
        throw new LogicException('The first character difference is not numeric');
    }

    // Get sequence
    $length = strspn($startString, '1234567890', $position);
    $prefix = substr($startString, 0, $position);
    $suffix = substr($startString, $position + $length);
    $start  = substr($startString, $position, $length);
    $end    = substr($endString, $position, $length);

    if ($start < $end) {
        if ($step <= 0) {
            throw new LogicException('Step must be positive');
        }
        for ($i = $start; $i <= $end; $i += $step) {
            yield $prefix . str_pad($i, $length, "0", STR_PAD_LEFT) . $suffix;
        }
    } else {
        if ($step >= 0) {
            throw new LogicException('Step must be negative');
        }
        for ($i = $start; $i >= $end; $i += $step) {
            yield $prefix . str_pad($i, $length, "0", STR_PAD_LEFT) . $suffix;
        }
    }
}

Only works when the ranges are equal length at the moment 仅在当前范围相等时有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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