错误:警告:strpos()[function.strpos]:偏移量不包含在字符串中-找不到解决方案 [英] Error : Warning: strpos() [function.strpos]: Offset not contained in string - can not find solution

查看:436
本文介绍了错误:警告:strpos()[function.strpos]:偏移量不包含在字符串中-找不到解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,有人问过这个问题,但不幸的是,没有解决该问题的答案.

I know, this question has been asked, but unfortunately, there are no answers how to solve this problem.

这出现在我的日志文件中:

This appears in my logfiles:

PHP消息:PHP警告:strpos():偏移不包含在第479行上的...的字符串中

不幸的是,我不明白是什么原因导致了这个问题以及如何解决.我多次测试了此功能(使用大的$ text,短的$ text,带有$ spam单词和没有$ spam单词),但我从未遇到此错误.那么,我的用户提交的哪种文本会导致此错误?

Unfortunately, I can not understand what causes this problem and how to fix it. I tested this function many times (with large $text, with short $text, with $spam words and without $spam words) but I never get this error. So, what kind of texts my users submit that cause this error?

    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":

For some reason this function does not filter the first word of the string. For example in this string function does not find word "hotmail":

$text = 'hotmail test test test test';

但在此字符串中却找到单词"hotmail":

but in this string it finds word " hotmail":

$text = 'test hotmail test test test test';

推荐答案

第一个问题:

很可能在某一时刻将空字符串传递给函数. strpos调用中的Offset指示应从哪个字符开始搜索$needle.它是从0开始的,因此,如果要从绝对开头开始,则可以将其设置为0或将其省略(默认为0.)

Most likely at one point you're passing an empty string to your function. Offset in the strpos call indicates from which character it should start searching for $needle. It's 0-based, so if you want to start from the absolute beginning, you either set it to 0 or omit it (it defaults to 0.)

第二个问题:

如前所述,偏移量是从0开始的,因此,如果要搜索的$needle恰好在$haystack的开头,则$offset为1则找不到.就像您正在搜索的字符串是这样的:'otmail test test test test'.

As mentioned before, the offset is 0-based, so if $needle you're searching for is exactly in the beginning of $haystack, it cannot be found if $offset is 1. With $offset = 1 it would be as if you're searching in a string that looks like this: 'otmail test test test test'.

还有一件事:

我建议您出于目的使用stripos而不是strpos,因为它不区分大小写,并且如果需要,还可以找到带有大写字母的单词.

I suggest you should use stripos, not strpos for your purposes, as it is case-insensitive and will also find words with uppercase letters, if it's something you might need.

这篇关于错误:警告:strpos()[function.strpos]:偏移量不包含在字符串中-找不到解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆