在php中按一定值递增数字等字母
如果我写在php中按一定值递增数字等字母,php,phpexcel,Php,Phpexcel,如果我写 $c='A' $c++ 它增加到'B',但如果我想增加2,3或更多, 例如:$c+2或$c+3,以获得备用字母表 for ($column = 'B'; $column < $highestColumn; $column++) { $cell = $worksheet->getCell($column.$row); $cell2=$worksheet->getCell($column+1.$row); } for($column='B';$column
$c='A'
$c++代码>
它增加到'B',但如果我想增加2,3或更多,
例如:$c+2或$c+3,以获得备用字母表
for ($column = 'B'; $column < $highestColumn; $column++) {
$cell = $worksheet->getCell($column.$row);
$cell2=$worksheet->getCell($column+1.$row);
}
for($column='B';$column<$highestColumn;$column++){
$cell=$worksheet->getCell($column.$row);
$cell2=$worksheet->getCell($column+1.$row);
}
但是$column+1不起作用
如何操作?如果要将字符增加2,则添加将不起作用
请尝试以下方法:
echo chr(ord($c) + 2)
说明:
使用ord($c)计算$c的ascii值
将ascii值加2
使用chr()函数将此achii值转换为字符串
请参阅和功能
注意:
按照规定,这只适用于Z,不适用于Z以外的位置。递增字母只适用于+
递增运算符,而不适用于+
加法
如果要继续使用增量门,可以使用循环:
$column = 'AH';
$step = 7; // number of columns to step by
for($ = 0; $i < $step; $i++) {
$column++;
}
它的另一个好处是允许您对($column='B';$column<$highestColumn;$column++){$cell=$worksheet->getCell($column.$row);}使用负阶跃值,这样我就可以获得BC、DE、fg列的成对值。谢谢,非常有用
$column = 'AH';
$step = 7; // number of columns to step by
$columnNumber = PHPExcel_Cell::columnIndexFromString($column) + $step;
$column = PHPExcel_Cell::stringFromColumnIndex($columnNumber - 1);