【问题标题】:preg_match: undefined offsetpreg_match:未定义的偏移量
【发布时间】:2011-12-30 02:17:36
【问题描述】:

这给了我一个错误,例如“注意:未定义的偏移量:第 12 行的 \index.php 中的 0”。

$arr = array('a', 'b', 'c', 'd');

$i=0;
while($i<=3){
    foreach($arr as $value){
        //get data
        $content = file_get_contents('http://www.example.com/$value');

        preg_match("^ y: ....^", $content, $match);
        $string = $match[$i];

        $i++;

        echo $value;
        echo $string;
    }
}

【问题讨论】:

  • $match 与您使用它的方式不同——它包含子模式匹配。我建议您在修改代码之前仔细阅读PCRE documentation。
  • 你不需要 while 循环

标签: php preg-match undefined offset


【解决方案1】:

反斜杠之前:(也许是第二个^?)

【讨论】:

  • 我认为正则表达式没有问题:\
  • 错误:$match 是一个由 preg_match 填充的数组。 $match[0] 将是总匹配,$match[1] 是第一个带括号的表达式(组),依此类推。您的 $i 循环假设有四个组。我本来希望在 foreach 中使用 $i 循环,或者甚至可能是 $match[0] 左右。 ^ 是开始,也许第二个应该是 $(结束),并且使用 "a:{3}" 表示重复 a 三次。见php.net/manual/en/function.preg-match.php。
【解决方案2】:

您是否尝试过使用

print_r($arr);

检查数组$arr,它可以帮助你找出问题所在。

它似乎是指 - $arr[0] as $arr[1]

【讨论】:

  • 数组好像没有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 2011-03-13
  • 2022-01-02
  • 2013-01-25
  • 2011-07-22
相关资源
最近更新 更多