php 错误:警告:strpos() [function.strpos]:字符串中不包含偏移量 - 找不到解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16251501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:46:20  来源:igfitidea点击:

Error : Warning: strpos() [function.strpos]: Offset not contained in string - can not find solution

php

提问by user1406271

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

我知道,这个问题已经被问到了,但不幸的是,没有答案如何解决这个问题。

This appears in my logfiles:

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

PHP message: PHP Warning: strpos(): Offset not contained in string in ... on line 479

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

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?

不幸的是,我不明白是什么导致了这个问题以及如何解决它。我多次测试了这个函数(用大 $text,用短 $text,用 $spam 词和不带 $spam 词),但我从来没有收到这个错误。那么,我的用户提交的哪些类型的文本会导致此错误?

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

Second question:

第二个问题:

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

出于某种原因,此函数不会过滤字符串的第一个单词。例如在这个字符串函数中找不到单词“hotmail”:

$text = 'hotmail test test test test';

but in this string it finds word " hotmail":

但在这个字符串中,它找到了“hotmail”这个词:

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

采纳答案by pilsetnieks

To the first question:

对于第一个问题:

Most likely at one point you're passing an empty string to your function. Offsetin the strposcall 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.)

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

To the second question:

对于第二个问题:

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

如前所述,偏移量是从 0 开始的,因此如果$needle您要搜索的内容恰好位于 的开头$haystack,则如果$offset为 1 ,则无法找到它。$offset = 1这样就好像您在搜索这样的字符串一样:'otmail test test test test'

One more thing:

还有一件事:

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

我建议你应该使用stripos, 而不是strpos为了你的目的,因为它不区分大小写,如果你可能需要的话,它也会找到带有大写字母的单词。

回答by Alex

The offset value is higher than the length of the string to search in.

偏移值大于要搜索的字符串的长度。

回答by maringtr

To answer your second question - you have two issues in your code. Assuming your text string is:

要回答您的第二个问题 - 您的代码中有两个问题。假设您的文本字符串是:

$text = 'hotmail test test test test';

..and your if statement is

..你的 if 语句是

if (strposab($text, $spam, 1))

First, you're starting at offset 1, while the word "hotmail" is at position 0. So by specifying an offset of 1, you're checking against the String:

首先,您从偏移量 1 开始,而单词“hotmail”位于位置 0。因此,通过指定偏移量 1,您正在检查字符串:

otmail test test test test

...and not

...并不是

hotmail test test test test

Second of all, with "hotmail" being at position 0, your strposab() function will return a value of int(0), which is a non-Boolean value that when used in a Boolean expression evaluates to FALSE. Therefore, you need to use the !== operator in order to avoid type juggling. So the correct if statement to use will be:

其次,当“hotmail”位于位置 0 时,您的 strposab() 函数将返回一个 int(0) 值,这是一个非布尔值,在布尔表达式中使用时计算结果为 FALSE。因此,您需要使用 !== 运算符以避免类型杂耍。因此,要使用的正确 if 语句将是:

if (strposab($text, $spam, 0) !== false)