php试图检查用户';的输入不包含禁止的单词
尝试检查用户输入的内容是否不包含禁止的单词 创建的数组,例如php试图检查用户';的输入不包含禁止的单词,php,Php,尝试检查用户输入的内容是否不包含禁止的单词 创建的数组,例如$probited_words=array{'hell'} 然后用户的输入$input='Hello!' 和php foreach ($prohibited_words as $one_prohibited_word) { if( stristr( substr( $input ), $one_prohibited_word ) !== FALSE ) { $error_message_words = 1; echo
$probited_words=array{'hell'}代码>
然后用户的输入$input='Hello!'代码>
和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/>';
}
}
访客输入
$user_input = ' Hello b a d word go g g g on a a a again';
首先,我替换空白。如果超过一个空格,则替换为一个空格(我相信在正常情况下一句话中不能超过一个空格)
然后根据空白创建数组
在这里,我创建了一个新数组,没有空元素,也没有更改键顺序
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);
}
}
然后通过数组的每个元素进行循环
如果特定元素的字符串长度为1(一个字母中只有一个字母/单词),则检查上一个循环中的字母数。若在前一个循环中也有一个字母,那个么将它们放在一起并创建新单词
$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
不幸的是,如果$user_input='Hello ba d go g on a agree'代码>
看来很难达到要求的结果。在花了大量时间编写代码之后,可以看到访问者可以发送带有文本的消息,从中可以很容易地理解它包含了“坏单词/坏意思”。所以,总结一下,如果我已经有一些代码可以过滤一些东西,我会使用这些代码。但在其他方面,试图创建一些代码似乎是不合理的。另一种方法是必须过滤垃圾邮件发送者和不良文本发送者。可能会执行类似操作,将输入拆分为一个单词数组,然后检查输入数组中是否有任何单词也出现在$banforted\u 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/>";
}
可以这样做,将输入拆分为一个单词数组,然后检查输入数组中是否有任何单词也出现在$probited\u 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/>";
}
不可错过的可能重复:]这在不可错过的可能重复之前已经回答过:]这在这种情况下似乎不容易解决。例如,如果输入他会问你怎么样代码>。可以首先创建数组,如您的答案explode(“”,$input)
中所示。然后,如果某个数组元素只有一个字符,则检查以下数组元素。如果还有一个字符,那么就放在一起。但是也没有解决方案,输入会因为感叹号而失败。您可以选择删除“Hello!”中的感叹号在$input中输入禁止的单词,或者决定从$input中删除所有标点符号,这样无论单词的标点符号从何处删除,单词都会匹配?如下面的$INPUBLED_单词=数组('Hello')$输入='他会问你怎么样'代码>啊,我在strpos()中的参数顺序错误。看看我的编辑似乎不容易解决这种情况。例如,如果输入他会问你怎么样代码>。可以首先创建数组,如您的答案explode(“”,$input)
中所示。然后,如果某个数组元素只有一个字符,则检查以下数组元素。如果还有一个字符,那么就放在一起。但是也没有解决方案,输入会因为感叹号而失败。您可以选择删除“Hello!”中的感叹号在$input中输入禁止的单词,或者决定从$input中删除所有标点符号,这样无论单词的标点符号从何处删除,单词都会匹配?如下面的$INPUBLED_单词=数组('Hello')$输入='他会问你怎么样'代码>啊,我在strpos()中的参数顺序错误。查看我的编辑
$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/>";
}
}