【问题标题】:Looping through letters not working遍历字母不起作用
【发布时间】:2013-08-26 21:08:22
【问题描述】:

我有一个函数,它循环遍历 excel 列。它昨天工作,但现在我遇到了问题。根据我的var_dump(),我的函数正在返回false 它甚至没有进入 for 循环(我已经在该循环中回显了“这里”并且没有回显)。为什么它不起作用?

$max_col 确实返回正确的最大值列

function get_col(PHPExcel $excel, $search, $row = 5, $col = "A"){
    $max_col = (string)$excel->getActiveSheet()->getHighestColumn(); // returns BH
    for($i = (string)$col; $i <= $max_col; $i++){
        $val    = trim($excel->getActiveSheet()->getCell("{$i}{$row}")->getValue());
        $search = preg_quote($search);
        if(preg_match("/$search/isU", $val)){
            return "$i";
        }
    }
    return false;
}

这是我调用函数的方式:

$col = get_col($excel, $sku, 5, "Q");
var_dump($col);

【问题讨论】:

    标签: php string for-loop


    【解决方案1】:

    对字符串使用&lt;= 比较会导致问题,因为它是字母比较,并且按字母顺序“C”>“BH” 将 $max_col 的初始值调整为超出您要检查的最大值的一列,然后使用!== 比较

    function get_col(PHPExcel $excel, $search, $row = 5, $col = "A"){
        $max_col = $excel->getActiveSheet()->getHighestColumn(); // returns BH
        $max_col++;
        for($i = $col; $i !== $max_col; $i++){
            $val    = trim($excel->getActiveSheet()->getCell("{$i}{$row}")->getValue());
            $search = preg_quote($search);
            if(preg_match("/$search/isU", $val)){
                return "$i";
            }
        }
        return false;
    }
    

    【讨论】:

    • 完美!这解决了它!
    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2015-01-04
    • 2013-06-15
    相关资源
    最近更新 更多