Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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
Php 始终为无效用户名,带/不带脏话_Php_Passwords - Fatal编程技术网

Php 始终为无效用户名,带/不带脏话

Php 始终为无效用户名,带/不带脏话,php,passwords,Php,Passwords,我正试图写一段代码,拒绝任何包含坏单词的用户名。 无论我做什么,我都会得到“无效用户名” 我目前正在学习php,并且已经尝试了所有我能想到的方法让它工作-帮助 该函数工作正常,但您以不正确的方式调用它,因为如果匹配了一个坏字,该函数将返回False: if( teststringforbadwords( $wantusername, $banned_words ) ) { echo 'string is clean'; } else { echo('string contains

我正试图写一段代码,拒绝任何包含坏单词的用户名。 无论我做什么,我都会得到“无效用户名”


我目前正在学习php,并且已经尝试了所有我能想到的方法让它工作-帮助

该函数工作正常,但您以不正确的方式调用它,因为如果匹配了一个坏字,该函数将返回
False

if( teststringforbadwords( $wantusername, $banned_words ) )
{
    echo 'string is clean';
}
else
{
    echo('string contains banned words');
    $message = "Invalid username.";
}

否则,如果要保持与函数名的一致性,则必须反转函数内的
True
False
返回值。

哪些值
$banked\u words
$wantusername
有?另外,除了
foreach
循环之外,您只需要
if(在数组($wantusername,$banked_words)){return false;}或者{return true;}
@A-2-A或者更短,
return!在数组中($wantusername,$banked_单词)
FYI,
fread()
后接
explode)
相当于
file()
@A-2-A
在数组中
区分大小写。@A-2-A
在数组中
查找精确匹配,
stristr
匹配干草堆中的任何地方。谢谢,我决定不使用@fopen,而是在文件中声明被禁止的单词数组。例如:$banked_words=数组(“*”,……)@杜威:这很奇怪。我已经用
fopen
测试了你的脚本,它对我有效。。。您确定单词前后没有空格,并且您的尾行字符是lf(
\n
)?这很奇怪,但我一定是做错了什么-当我重新键入它时,它工作正常。可能有空间。谢谢
if( teststringforbadwords( $wantusername, $banned_words ) )
{
    echo 'string is clean';
}
else
{
    echo('string contains banned words');
    $message = "Invalid username.";
}