未定义的偏移量:1

3
在我的当前PHP脚本中出现了这个错误:Undefined offset: 1。
我的代码在这里:
$query = "SELECT item_id, username, item_content FROM updates ORDER BY update_time DESC                 LIMIT " . $start . ", " . $number_of_posts;

    $result = mysql_query($query) or die(mysql_error());        
    while($row = mysql_fetch_assoc($result)) {
            preg_match("/<p>(.*)<\/p>/",$row['item_content'],$matches);
            $row['item_content'] = strip_tags($matches[1]);
            $posts[] = $row;
        }

如果您知道导致此问题的原因,请在下面发表评论以提供帮助。谢谢! :)

2
只返回翻译后的文本:并且接受一些人们想要帮助你更多的答案 ^_^ - Naftali
3个回答

4

与其

$row['item_content'] = strip_tags($matches[1]);

尝试

if (isset($matches[0]) && isset($matches[0][1]))
  $row['item_content'] = strip_tags($matches[0][1]);
else
  $row['item_content'] = '';

1

错误出现在这一行:

$row['item_content'] = strip_tags($matches[1]);

同意,我认为这是 strip_tags 的不当使用 -- strip_tags 应该用于整个字符串 -- 如果 $matches 是一个字符串(根据 preg_match 行判断应该是),那么 $matches[1] 只会是一个单独的字符,对吧? - RussellUresti

0
正如Neal所说,该行存在问题。$matches [1] 似乎未定义(即没有匹配项,或只有1个匹配项 $matches [0])。请确保您的表格正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接