Restricting dirty words in forms in PHP

It is necessary to restrict inappropriate words on the website. Furthermore, the owner of a forum-like website certainly doesn’t want their users to post dirty words. There are many things website owners restrict words, not even just dirty words.
This time, I’m going to share a PHP script to cut down bad words on the submission form. Usually forums like websites limit badwords as censored with an asterisk
but what I’m going to share is something else, which is when the user writes filtered words, an error message appears.
Here is the script, and a little explanation is below.
form.html
function validasi($string,$banned_words) {foreach($banned_words as $banned_word) {if(stristr($string,$banned_word)){return false;}}return true;}$banned_words = array('badword','badword2','badword3','badword4','badword5','badword6','badword7');$teks = $_POST['teks'];if (!validasi($teks,$banned_words)) {echo '';}else{echo 'Text valid';}?>
Validation.php
Explanation • form.html
– The input form that leads to Validation.php is in the form of POST data. • Validation ()
– The function is in the Validation.php to separate badwords. • $ banned_word
– To filter unwanted words and form an array. Please replace the text as in the example. • $ text
– To get POST data from the form • otherwise
– Call from the function validation (). To separate badwords and display warnings.The result of the above script when the user writes a filtered word is shown “The text you entered is blocked by the system“And will be redirected to the Google website. If the user does not write the filtered word in the meantime, “Valid text
“.
You can develop it to be used on your website and all that really is needed from the script above is the validation () function.
If you have anything to ask, please comment. ️
Hopefully useful and good luck