Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPs strpos不能按预期使用双引号字符串_Php_String_Strpos - Fatal编程技术网

PHPs strpos不能按预期使用双引号字符串

PHPs strpos不能按预期使用双引号字符串,php,string,strpos,Php,String,Strpos,如果PHP8.0中的字符串包含子字符串,我将使用以下代码返回true或false <?php $username = "mothertrucker"; // This username should NOT be allowed $banlistFile = file_get_contents("banlist.txt"); //Contains the word "trucker" in it $banlist = explod

如果PHP8.0中的字符串包含子字符串,我将使用以下代码返回true或false

<?php
$username = "mothertrucker"; // This username should NOT be allowed
$banlistFile = file_get_contents("banlist.txt"); //Contains the word "trucker" in it
$banlist = explode("\n", $banlistFile); // Splits $banlistFile into an array, split by line

if (contains($username, $banlist)) {
    echo "Username is not allowed!";
} else {
    echo "Username is allowed";
}

function contains($str, array $arr)
{
    foreach($arr as $a) { // For each word in the banlist
        if (stripos($str, $a) !== false) { // If I change $a to 'trucker', it works. "trucker" does not
            return true;
        }
    }
    return false;
}
?>

这是为了检测在创建用户名时是否使用了不合适的单词。例如,如果有人输入用户名“mothertrucker”,并且“trucker”包含在禁止列表中,我希望它拒绝它

现在有了这段代码,如果我只是输入“trucker”作为用户名,它就会被找到并阻止。酷。然而,如果字符串不仅仅是“卡车司机”,它不会检测到它。所以用户名“mothertrucker”是允许的

我发现,如果我在stripos函数中显式地键入'trucker'而不是$a,它就可以完美地工作。但是,如果我显式地输入“trucker”(带双引号),它将停止工作,并且仅当用户只输入了“trucker”时才会阻塞

所以我看到的是,我传递给它的字符串$a被PHP解释为双引号字符串,为了正确地检测它,它需要是单引号字符串。但据我所知,我无法控制php如何传递变量

我能把它转换成一个带引号的字符串吗?可能是我在第2行中使用的explode命令导致了它?有没有其他方法可以从txt文档中提取数据并将其解释为单引号字符串?希望我的解释是有道理的,但是你可以复制和粘贴代码,自己看看


谢谢你的帮助

我不知道您的文件实际包含什么,所以我不知道分解的结果是什么

无论如何,我的建议是(取决于您希望执行此操作的速度,以及banlist文件的长度以及您的禁止级别)不要分解文件,而是将其作为一个整体进行查看

<?php
$username = "allow"; // This username should be allowed
$banlist = "trucker\nmotherfucker\n donot\ngoodword";
var_dump(contains($username, $banlist));


function contains($str, $arr)
{
    if (stripos($arr, $str) !== false) return true;
    else return false;
}
?>


否则,如果您要允许say
good
,这是一个允许的单词,但由于它在带有
goodword
的文件中,因此不会(使用我的示例),您不应该使用
stripos
,而是使用您的示例并使用
stracecmp

一个潜在的问题是任何空格(包括
\r
之类的内容)可能会停止单词匹配,因此只需修剪要与之比较的单词就可以将其清理干净

stripos($str, $a)


你能试着用stripos($str,trim($a))吗
因为这将确保被禁止的单词没有任何额外的空格等。就是这样!我怀疑有额外的字符,但认为这不可能,因为我认为爆炸函数会被删除。\n也许它仍然在那里。怎么可能?非常感谢!!!我使用了stristr()对于bleeping单词,因为这个词不在乎是小写还是大写,也不在乎单词的前后,它会找到bleeping单词
stripos($str, trim($a))