警告:strpos():偏移量未包含在字符串

时间:2017-02-01 18:10:38

标签: php string strpos

当我对单个字符串使用strpos()时,它工作正常但是当我从URL获取html并将此html传递给此函数时,此错误显示。我无法理解这背后的原因。请有人解释这个警告吗?

$html = file_get_content("site URL");
function getHtml ($html, $startTag, $endTag, $start) {
        $tagStart = strpos($html, $startTag) + $start;
        $tagEnd = strpos($html, $endTag, $tagStart);
        return substr($html, $tagStart, $tagEnd - $tagStart);
}

并且错误指向此函数中的$ tagEnd行。

1 个答案:

答案 0 :(得分:1)

来自documentation comments

  

如果偏移量不在0和字符串长度之间,则此函数会发出警告:

     

Warning: strpos(): Offset not contained in string in ...