Offset not contained in string.¶
Description¶
strpos() searches for a string called $needle
in another string called $haystack
, starting at the offset passed as third argument, and called $offset
.
The offset must be smaller than the full length of the $haystack
: otherwise, PHP starts to search beyond the end of the data, and that will always fail.
This error message is shared by several PHP native and extension functions, namely mbstring
and iconv
: strpos()
, strrpos()
, stripos()
, strripos()
, mb_strpos()
, mb_strrpos()
, mb_stripos()
, mb_strripos()
, iconv_strpos
and iconv_strrpos
.
Before PHP 8.0, the failure was reported as a non-blocking warning. Since PHP 8.0, it is a fatal error.
Example¶
<?php
var_dump(strpos('abc', 'a', 4));
?>
Solutions¶
Check that the offset is not beyond the length of the string.
Use a try catch around this methodcall.
Lengthen the source string.
In previous PHP versions, this error message used to be %s(): Argument #%d ($%s) must be contained in argument #%d ($%s).
In more recent PHP versions, this error message is now %s(): Argument #%d ($%s) must be contained in argument #%d ($%s).