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

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

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

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

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

$text = 'hotmail test test test test'; 

but in this string it finds word " hotmail":

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

回答1:

To the first question:

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.)

To the second question:

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'.

One more thing:

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.



回答2:

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



回答3:

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

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:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!