dtufl26404 2018-01-09 01:09
浏览 9
已采纳

如何检查坏词的用户名? [重复]

This question already has an answer here:

SOLVED: Using Answer from Michael Thompson and "Duplicate" Question Users Flagged

All of the code used below is in PHP!

This is not the same as other questions asked on Stack Overflow I have tried the examples people have linked and nothing will work for me!

Prior to Asking on StackOverflow

I've googled this quite a few times, and tried many different tutorials, all of them being different from the other and making it more confusing. Using code like preg_match(), strpos() etc. and can't get it quite right.

Main Question:

I have a registration form on my site. For example, if the user were to sign up with a username like Bad or BadWord, I want it to stop them from doing so.

I have an array of bad words:

$badWords = array('some', 'bad', 'words');

My username variable is:

$username

I want to do a check, for example, if Bad or BadWords contains a word from the array, if it does then it will echo 'bad word detected!'

Pseudo code for example:

if ($username contains $badWords) {
     echo 'bad word detected!';
}

I don't have the greatest knowledge of PHP as I only began to learn it a few months ago, that is why I am asking for help.

Thanks!

</div>
  • 写回答

1条回答 默认 最新

  • dongpenggan6812 2018-01-09 01:23
    关注

    The simplest way would be to loop through the array with a for loop, and then check if each index exists inside the string. That can be achieved like so:

    $badWords = array('fk', 'st', 'ct', 'bd', 'dk');
    $containsBadWord = false;
    foreach ( $badWords as $badWord ) {
        if ( stripos($username, $badWord) ) {
            $containsBadWord = true;
            break; //We do this just to save a few loops where unneccesary
        }
    }
    if ( $containsBadWord ) {
        echo 'That username contains a word or words that are undesirable. Please pick a different username.';
    } else {
        echo 'That username does not contain a word or words that are undesirable, and you can use that.';
        //Provided that the username passes other checks such as whether it has not been used before
    }
    

    I would suggest using stripos in this context instead of strpos, due to the case insensitive nature of stripos. Also, if you do not understand the foreach, click the link to learn more about it on the PHP website. The foreach behaves the same here as a for loop as below:

    $count = count($badWords);
    for ( $i=0; $i<$count; $i++ ) {
        if ( stripos($username, $badWords[$i]) ) {
            $containsBadWord = true;
            break; //We do this just to save a few loops where unneccesary
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP