警告:strpos():WAMP中的字符串中不包含偏移量?

时间:2013-10-21 21:02:05

标签: php string strpos

我在WAMP中遇到奇怪的错误但在我的托管上没有,错误是:

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

功能是:

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>';
}

第292行是:

$cnt = substr($content, 0, strpos($content, '/', strpos($content, '/')+2));

1 个答案:

答案 0 :(得分:0)

这似乎是一个警告。我猜您在WAMP中启用了警告,您可以使用error_reporting();方法禁用它们:

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

注意:调用此方法一次将在PHP中永久设置此选项,因此如果您想再次显示警告,则必须在以后使用相同的方法使用不同的参数。

现在关于问题本身:

我认为你使用的偏移量参数存在问题:

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

以下方法中的[OFFSET]值:

  

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

可能比上下文字符串本身大。您可以通过在页面上打印偏移值和上下文字符串来轻松地调试它。

如果这不能解决问题,可能会返回此错误,因为在使用strpos时在字符串中找不到字符。您应该尝试分解更多行中使用的方法,以便根据警告返回的行号来解决问题。

希望这有帮助!

相关问题