生成两个字母数字值之间的范围
Generate a range between two alphanumeric values
我有一组数据,其中每个项目都有一个 'range from' 和 'range to' 不同类型的字符串。例如,第一个项目可能有 3001A -> 4000A,下一个项目可能是 DE25500 -> DE27419 等等(有几种不同的模式,但通常由一个静态部分和一个范围部分组成(似乎通常任何字母都是静态的并且数字可以是静态的也可以不是。
PHP 中是否有任何现成的内置函数可以处理生成范围内的中间值?或者如果没有关于如何构建一个的任何提示?
我不是 PHP 专家,但你不能为此使用 for 循环。
您需要提取代表范围的部分值,然后从最低到最高开始循环。
$min = (int) substr("DE25500", 2)
$max = (int) substr("DE27419", 2)
for ($x = $min; $x <= $max; $x++) {
// logic in here
}
我建议您首先找到所有模式,然后编写知道如何将数字部分与文字部分细分的函数。
然后要使用 PHP 中的范围,您可以使用 http://php.net/range
最后想出一个函数来做这件事...
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, "[=10=]")));
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;
}
}
}
目前只有当范围长度相等时才有效
我有一组数据,其中每个项目都有一个 'range from' 和 'range to' 不同类型的字符串。例如,第一个项目可能有 3001A -> 4000A,下一个项目可能是 DE25500 -> DE27419 等等(有几种不同的模式,但通常由一个静态部分和一个范围部分组成(似乎通常任何字母都是静态的并且数字可以是静态的也可以不是。
PHP 中是否有任何现成的内置函数可以处理生成范围内的中间值?或者如果没有关于如何构建一个的任何提示?
我不是 PHP 专家,但你不能为此使用 for 循环。
您需要提取代表范围的部分值,然后从最低到最高开始循环。
$min = (int) substr("DE25500", 2)
$max = (int) substr("DE27419", 2)
for ($x = $min; $x <= $max; $x++) {
// logic in here
}
我建议您首先找到所有模式,然后编写知道如何将数字部分与文字部分细分的函数。 然后要使用 PHP 中的范围,您可以使用 http://php.net/range
最后想出一个函数来做这件事...
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, "[=10=]")));
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;
}
}
}
目前只有当范围长度相等时才有效