简体   繁体   English

php试图检查用户的输入是否包含禁止的单词

[英]php trying to check if user's input does not contain banned words

Trying to check if user's input does not contain banned words 试图检查用户的输入是否包含禁止的单词

Created array, for example $prohibited_words = array{'hell'}; 创建数组,例如$prohibited_words = array{'hell'};

Then user's input $input = 'Hello!'; 然后用户输入$input = 'Hello!';

And php 和PHP

foreach ($prohibited_words as $one_prohibited_word) {
  if( stristr( substr( $input ), $one_prohibited_word ) !== FALSE ) {
    $error_message_words = 1;
    echo $one_prohibited_word. ' $one_prohibited_word <br/>';
  }
}

Here Hello appears as banned. 在这里Hello显示为禁止。

May change something like if( stristr( substr( $input ), ($one_prohibited_word.' ') ) !== FALSE ) { 可能会改变if( stristr( substr( $input ), ($one_prohibited_word.' ') ) !== FALSE ) {

But may be some better solution? 但可能是一些更好的解决方案?

Update Created possibly a bit crazy code... 更新创建可能有点疯狂的代码...

$bad_words_arr = array{'bad'};

Visitor's input 访客的意见

$user_input = ' Hello b a d word go  g g g on  a a a  again';

At first i replace whitespaces. 起初我替换空格。 If more than one whitespace, then replace with one whitespace (i believe in normal situation in a sentence must not be more than one white space). 如果有多个空格,则用一个空格替换(我相信句子中的正常情况不能超过一个空格)。

Then create array based on whitespaces. 然后基于空格创建数组。

Here i create new array without empty elements and changing key order. 在这里,我创建没有空元素和更改键顺序的新数组。

foreach( explode (' ', str_replace('  ',  ' ', $user_input) ) as     $value_new_arr ){
  if( strlen(trim($value_new_arr)) > 0 ){
    $arr_from_message_textarea[] = trim($value_new_arr);
  }
}

Then loop trough each element of array. 然后循环通过数组的每个元素。

If string length for particular element is one (only one letter/word from one letter), then check number of letters in the previous loop. 如果特定元素的字符串长度为1(一个字母只有一个字母/单词),则检查前一个循环中的字母数。 If in previous loop also one letter, then put them together and create new word. 如果在之前的循环中也有一个字母,那么将它们放在一起并创建新单词。

$temporary_string = '';
$new_word_already_started = 0;
foreach( $arr_from_message_textarea as $k_arr_from_message_textarea => $v_arr_from_message_textarea ){

if( $k_arr_from_message_textarea > 0 ){

  if( strlen($v_arr_from_message_textarea) == 1  ){//current element has only one letter

    if( strlen( $arr_from_message_textarea[$k_arr_from_message_textarea-1] ) == 1 ){//and previous element has only one letter

      if( $new_word_already_started == 0 ){
      $temporary_string = $arr_from_message_textarea[$k_arr_from_message_textarea-1]. $v_arr_from_message_textarea;//I started to create word. Put together two first letters
      $new_word_already_started = 1;
      }
      else if( $new_word_already_started == 1 ) {
      $temporary_string = $temporary_string.$v_arr_from_message_textarea;//Take previous letters 
      }

    }//if( strlen( $arr_from_message_textarea[$k_arr_from_message_textarea-1] ) == 1 ){
    else if( strlen( $arr_from_message_textarea[$k_arr_from_message_textarea-1] ) > 1 ){
    $temporary_string = '';//reset temporary string to start to use empty string
    $new_word_already_started = 0;
    }

  }//if( strlen($v_arr_from_message_textarea) == 1  ){

  else if( strlen($v_arr_from_message_textarea) > 1  ){//Imagine, in some previous loops were only one letter, created $temporary_string. In this loop word has more than one letter. So word from previous loops is created. Pass the word to array $new_word and reset $temporary_string to empty.
    if( strlen($temporary_string) > 0 ){
    $new_word[] = $temporary_string;
    }
  $temporary_string = '';
  $new_word_already_started = 0;
  }

}//if( $k_arr_from_message_textarea > 0 ){

}//foreach

if( isset($new_word) ){
//Merge initial array with the new array
$arr_to_check_bad_words = array_merge( ( explode (' ', str_replace( '  ',  ' ', str_replace('  ',  ' ', $user_input ) ) ) ), $new_word );
}
else{
$arr_to_check_bad_words = explode (' ', str_replace( '  ',  ' ', str_replace('  ',  ' ', $user_input ) ) );
}

$result_bad_words = array_intersect( $arr_to_check_bad_words, $bad_words_arr );// As understand get array of words from $arr_to_check_bad_words if the words exist in $bad_words_arr

Unfortunately this does not work if $user_input = ' Hello ba d go ggg on aaa again'; 不幸的是,如果$user_input = ' Hello ba d go ggg on aaa again';这不起作用$user_input = ' Hello ba d go ggg on aaa again';

It appears very difficult to get required result. 获得所需结果似乎非常困难。 And after spending lot of time to write code, can see that visitor can send message with text from which can easily understand that it contains "bad words/bad meaning". 在花了很多时间编写代码之后,可以看到访问者可以发送带有文本的消息,从中可以很容易地理解它包含“坏词/坏词”。 So conclusion, if i already have some code that filters something, i will use the code. 所以,如果我已经有一些过滤东西的代码,我将使用代码。 But otherwise seems unreasonable to try to create some code. 但尝试创建一些代码似乎是不合理的。 In another way must filter spammers and bad text senders. 另一种方式必须过滤垃圾邮件发送者和坏文本发件人。

Maybe do something like this, where you split the input into an array of words, and then check to see if any words in the input array are also present in the $prohibited_words array: 也许做这样的事情,你将输入分成一个单词数组,然后检查输入数组中的任何单词是否也出现在$prohibited_words数组中:

$prohibited_words = array('Hello!');
$input = 'Hello!';
$input_array = explode(' ', $input);
$intersect = array_intersect($prohibited_words, $input_array);

foreach ($intersect as $item) {
    echo "$item <br/>";
}

A more robust solution would involve removing all of the spaces from the input string, and then checking to see if any of the items in the $prohibited_words array appears in the edited input string. 更强大的解决方案将涉及从输入字符串中删除所有空格,然后检查$prohibited_words数组中的任何项是否出现在已编辑的输入字符串中。 You could also use strtolower() to make the search case-insensitive: 您还可以使用strtolower()来使搜索不区分大小写:

$prohibited_words = array('Hello!');    
$input = 'He ll   o!';
$new_input = str_replace(' ', '', strtolower($input));

foreach($prohibited_words as $item) {
    if (strpos($new_input, strtolower($item)) !== false) {
        echo "$item <br/>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM