我知道,这个问题已被问到,但不幸的是,没有答案如何解决这个问题。错误:警告:strpos()[function.strpos]:偏移不包含在字符串中 - 找不到解决方案
这出现在我的日志文件:
PHP消息:PHP的警告:strpos():偏移不包含在...串线479
不幸的是,我不明白是什么原因导致这个问题以及如何解决它。我多次测试过这个函数(大$文本,短$文本,$垃圾文字和没有$垃圾文字),但我从来没有得到这个错误。那么,我的用户提交哪种文本会导致此错误?
if (strposab($text, $spam, 1)) {
echo "Email addresses and URLs not allowed here";
die;
}
$spam = array('http','www','hotmail','yahoo','gmail','msn');
function strposab($haystack, $needles=array(), $offset=0) {
$chr = array();
foreach($needles as $needle) {
$res = strpos($haystack, $needle, $offset);
if ($res !== false) $chr[$needle] = $res;
}
if(empty($chr)) return false;
return min($chr);
}
第二个问题:
出于某种原因,该功能不会过滤字符串的第一个字。 例如,在此字符串函数没有找到单词 “的Hotmail”:
$text = 'hotmail test test test test';
但在此字符串找到词 “的Hotmail”:
$text = 'test hotmail test test test test';
[“*还要注意的是字符串位置从0开始,而不是1 。*“](http://us.php.net/strpos#refsect1-function.strpos-returnvalues)。您将'1'而不是'0'作为起始偏移量,并且您的用户正在提交空字符串/单个字符。 – DCoder 2013-04-27 11:34:21