警告: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行。

从文档注释:

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

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

最新更新