Trying to check if user's input does not contain banned words
Created array, for example $prohibited_words = array{'hell'};
Then user's input $input = 'Hello!';
And 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.
May change something like 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. 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 g g g on a a a 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.