好吧,我觉得这很奇怪,但应该有一个解释。这是发生了什么。意外的PHP数组关键行为
这里该代码应该没有什么呼应:
$str = '[email protected]';
$key = '11111';
echo strpos($str, $key);
exit;
..是的,这正是我所得到的,什么都没有。但是! 如果我使用$键(其中包含字符串)作为阵列的实际钥匙:
$str = '[email protected]';
$arr = array('11111' => 'test');
foreach ($arr as $key => $val)
{
echo 'String: '.$str.'<br>';
echo 'Key: '.$key.'<br>';
echo 'Found at position: '.strpos($str, $key);
}
exit;
我得到这个惊人的,不可思议的结果:
String: [email protected]
Key: 11111
Found at position: 2
那么PHP在这里找到被串是信g
但是,什么是更惊人的,是的位数改变了结果:
$str = '[email protected]';
$arr = array('111' => 'test');
foreach ($arr as $key => $val)
{
echo 'String: '.$str.'<br>';
echo 'Key: '.$key.'<br>';
echo 'Found at position: '.strpos($str, $key);
}
exit;
这一给出:
String: [email protected]
Key: 111
Found at position: 9
在这方面的专家? 谢谢。
编辑: 这是在我的项目中使用的实际代码例子给出了这样的误报:
$email = '[the email of the user here]';
$arr = array(
// [...]
'11111' => 'Banned',
'22222' => 'Banned',
'33333' => 'Banned',
// [...]
);
foreach ($arr as $key => $reason)
{
if (strpos($email, (string)$key) !== false)
{
return 'Keyword: '.(string)$key.' found in the user Email address with reason: '.(string)$reason;
}
}
因此,即使用(string)在变量$key
前它在登录表单
[与Strpos在PHP的问题(的可能的复制https://stackoverflow.com/questions/1039738/问题与strpos在PHP) – mickmackusa