drl37530 2016-03-16 23:53
浏览 348
已采纳

PHP警告:str_repeat():第二个参数必须大于或等于0 [关闭]

I'm getting this in my php-error log, im not sure where $pad is being set:

PHP Warning: str_repeat(): Second argument has to be greater than or equal to 0 in file.php on line 105

Here is the code:

    $tokens = array_filter(array_map('trim', explode("
", $xml)));

    $result     = ''; // holds formatted version as it is built
    $pad        = 0; // initial indent
    $matches    = array(); // returns from preg_matches()

    $insideCDATA = false;
    $currentCDATA = null;

    foreach($tokens as $token) {
        $closeCDATA = false;

        if (preg_match('/^<!\[CDATA.*\]\]>$/', $token, $matches)) :
            $indent = 0;
        elseif (preg_match('/^<!\[CDATA.*$/', $token, $matches)) :
            $insideCDATA = true;
            $currentCDATA = '';
        elseif (preg_match('/.+\]\]>$/', $token, $matches)) :
            $insideCDATA = false;
            $closeCDATA = true;
            $indent = 0;
        elseif (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) : 
            $indent = 0;
        elseif (preg_match('/^<\/\w/', $token, $matches)) :
            if (!$insideCDATA) {
                $pad--;
            }
            $indent = 0;
        elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
            $indent = 1;
        else :
            $indent = 0; 
        endif;

        if ($insideCDATA) {
            $currentCDATA .= $token;
        } else {
            if ($closeCDATA) {
                $token = $currentCDATA.$token;
                $currentCDATA = null;
            }
            $line    = str_repeat("\t", $pad).$token;
            $result .= $line . "
"; // add to the cumulative result, with linefeed
            $pad    += $indent; // update the pad size for subsequent lines    
        }
    }

    return $result;
  • 写回答

1条回答 默认 最新

  • doolo00026 2016-03-17 00:04
    关注

    You should make your code more stable, checking parameters prior to passing it to function. In your case you initialize $pad with zero, but then you can decrement this value and your second param to str_repeat can be less then zero.

    For example, in this case you can call str_repeat in this manner str_repeat( "\t", ($pad >=0) ? $pad : 0 )

    if ($insideCDATA) {
        $currentCDATA .= $token;
    } else {
        if ($closeCDATA) {
            $token = $currentCDATA.$token;
            $currentCDATA = null;
        }
        $line    = str_repeat( "\t", ($pad >=0) ? $pad : 0 ).$token;
        $result .= $line . "
    "; // add to the cumulative result, with linefeed
        $pad    += $indent; // update the pad size for subsequent lines
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题