Question

I am getting weird error in WAMP but not on my hosting, and the error is:

Warning: strpos(): Offset not contained in string in C:\wamp\www\wordpress\wp-content\themes\corpo-child\functions.php on line 292

Function is:

function url( $atts, $content = null ) {
$cnt = substr($content, 0, strpos($content, '/', strpos($content, '/')+2));
$cnt = str_replace('http://www.', '', $cnt);
$cnt = str_replace('http://', '', $cnt);
$cnt = str_replace('www.', '', $cnt);
$cnt = str_replace('embed.', '', $cnt);
    return '<div id="url"><a href="/external/?link='.$content.'" target="_blank">'.$cnt.'</a></div>';
}

Line 292 is:

$cnt = substr($content, 0, strpos($content, '/', strpos($content, '/')+2));
Was it helpful?

Solution

This seems to be a warning. I guess you have warnings enabled in WAMP, you can disable them using the error_reporting(); method:

// Report simple running errors
error_reporting(E_ERROR | E_PARSE);

Note: Calling this method once will set this option forever in PHP, so if you'd like to show warnings again, you have to use the same method with different arguments at a later time.

Now about the issue itself:

I think it's a problem with the offset argument you are using in:

$cnt = substr($content, 0, [OFFSET]);

This [OFFSET] value from the following method:

strpos($content, '/', strpos($content, '/')+2)

Is probably larger than the context string itself. You can debug this very easily by printing the offset value and the context string on the page.

If this doesn't solve the problem, maybe this error is returned because a character isn't found in the string when using strpos. You should try breaking up the methods you use in more lines so you can trouble shoot the problem based on the line number that the warning returns.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top