像数字一样在php中按数字递增字母 [英] Increment letters like number by certain value in php

查看:241
本文介绍了像数字一样在php中按数字递增字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写的话,在php中

In php if I writ

$c='A';
$c++;
它增加到"B",但如果我想将其增加2,3或更多, 例如:$ c + 2或$ c + 3,以获取备用字母

$c='A';
$c++;
it increments to 'B' but if I want to increment it with 2 ,3 or more , eg: $c+2 or $c+3 ,to get the alternate alphabets

 for ($column = 'B'; $column < $highestColumn; $column++) {   
 $cell = $worksheet->getCell($column.$row);
 $cell2=$worksheet->getCell($column+1.$row);
 }

但$ column + 1无效

but the $column+1 dont work

该怎么做?

推荐答案

递增字母仅适用于++增量运算符,不适用于+加法.

Incrementing letters will only work with the ++ incrementor operator, not with + addition.

如果要继续使用增量器,可以使用循环:

If you want to continue using an incrementor, you can use a loop:

$column = 'AH';
$step = 7; // number of columns to step by
for($ = 0; $i < $step; $i++) {
    $column++;
}

但是,PHP的字符增量器不会向后工作,因此您不能使用负步长值

However, PHP's character incrementor won't work backwards, so you couldn't use a negative step value

如果要使用加法而不是循环,则需要将该列转换为数值,进行加法,然后再转换回

If you want to use addition rather than a loop, then you need to convert that column to a numeric value, do the addition, then convert back

$column = 'AH';
$step = 7; // number of columns to step by
$columnNumber = PHPExcel_Cell::columnIndexFromString($column) + $step;
$column = PHPExcel_Cell::stringFromColumnIndex($columnNumber - 1);

具有允许您使用负步长值的其他优点

Which has the added benefit of allowing you to use negative step values

这篇关于像数字一样在php中按数字递增字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆