Mohsin Akhtar
Mohsin Akhtar

Reputation: 95

Warning: strpos(): Offset not contained in string

When I use strpos() for single string then it works fine but when I grab html from URL and pass this html to this function then this error shows. I am not able to understand the reason behind this. Please anyone explain this warning ?

$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);
}

and error points to the $tagEnd line in this function .

Upvotes: 2

Views: 2137

Answers (1)

BastianW
BastianW

Reputation: 2658

From the documentation comments:

This function raises a warning if the offset is not between 0 and the length of string:

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

Upvotes: 2

Related Questions