Block Offensive Words on Chat in PHP

0
53

Hello! Good day everyone! This tutorial is all about Block Offensive Words on Chat in PHP Today, I’m gonna teach you on creating a Block Offensive Words on Chat in PHP a chatbox in which offensive messages will be automatically block by the system. So by the user of the PHP Function called str_replace we can make it possible. Now, let’s start our tutorial.

So first, create a PHP function. Name it as function filter_words($statements) {}. 

Inside the filter_words function. Put the following codes.

  1. $offensive_words = array("Fuck You", "FUCK YOU", "Fuck you", "fuck you");
  2. $convert_word = array("**** ***", "**** ***", "**** ***", "**** ***");
  3. $new_word = str_replace($offensive_words, $convert_word, $statements);
  4. echo $new_word;

So, the variable $offensive_words are the list of all words formatted in array you want to block if you think that it can offend to the chatters. You can add as many as words you want to add on it.

Next is the $convert_word variable, these are the list of all words formatted in array that you want to replace on the offensive words.

The codes should be goes like this.

  1. function filter_words($statements) {
  2. $offensive_words = array("Fuck You", "FUCK YOU", "Fuck you", "fuck you");
  3. $convert_word = array("**** ***", "**** ***", "**** ***", "**** ***");
  4. $new_word = str_replace($offensive_words, $convert_word, $statements);
  5. echo $new_word;
  6. }

Now, we will create a chatbox with the use of the filter_words function to filter every words on the chat.

Create a database, name it as any name you desire. In my case. I use itsourcecode as the name of the database.

Then create a table name it as chats. Then put the following attributes.

  1. CREATE TABLE `chats` (
  2. `chat_id` int(11) NOT NULL,
  3. `chat_name` text NOT NULL,
  4. `chat_msg` text NOT NULL
  5. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now for the connection. Create a conn.php file then put the following codes

  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'itsourcecode');
  3.  
  4. if ($conn->connect_error) {
  5. echo $conn->error;
  6. }
  7. ?>

Then put the following codes on the index page.

  1. <?php
  2. include 'conn.php';
  3. function filter_words($statements) {
  4. $offensive_words = array("Fuck You", "FUCK YOU", "Fuck you", "fuck you");
  5. $convert_word = array("**** ***", "**** ***", "**** ***", "**** ***");
  6. $new_word = str_replace($offensive_words, $convert_word, $statements);
  7. echo $new_word;
  8. }
  9. ?>
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Block Offensive Words in PHP</title>
  5. <link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css"/>
  6. <style>
  7. body {
  8. padding-top: 10px;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="container">
  14. <div class="jumbotron">
  15. <form method="post" action="chat-action.php" role="form">
  16. <div class="panel panel-default">
  17. <div class="panel-body">
  18. <div class="row" style="margin-bottom: 10px">
  19. <div class="col-sm-4">
  20. <label>Type your name:</label>
  21.  
  22. </div>
  23. &lt;/div&gt;
  24. &lt;label&gt;Chat:&lt;/label&gt;
  25. &lt;input type="text" name="chat" class="form-control" /&gt;
  26. &lt;button type="submit" class="btn btn-primary" style="margin-top: 5px;"&gt;Submit&lt;/button&gt;
  27. &lt;/div&gt;
  28. &lt;/div&gt;
  29. &lt;/form&gt;
  30. <div class="panel panel-default">
  31. <div class="panel-body">
  32. query("SELECT * FROM chats");
  33. while ($chat_data = $chats-&gt;fetch_assoc()) { ?&gt;
  34. <img src="assets/images/user-ava.png" width="20px" alt="" />
  35. <b></b>
  36. <hr>
  37. num_rows == null) {
  38. echo 'no chats yet';
  39. }
  40. ?&gt;
  41. </div>
  42. &lt;/div&gt;
  43. &lt;/div&gt;
  44. &lt;/div&gt;
  45. &lt;/body&gt;
  46. &lt;/html&gt;

For the chat action. Create a chat-action.php file then put the following codes.

  1. &lt;?php
  2. include 'conn.php';
  3.  
  4. if (isset($_POST['chat'])) {
  5. $name = $_POST['name'];
  6. $msg = $_POST['chat'];
  7.  
  8. $chat = $conn-&gt;query("INSERT INTO chats (chat_name, chat_msg) VALUES ('$name', '$msg')");
  9. if ($chat) {
  10. header("Location: index.php");
  11. } else {
  12. echo $conn-&gt;error;
  13. }
  14. }
  15. ?&gt;

Done.

Screenshots: 

IF you have questions regarding this tutorial entitled as “Block Offensive Words on Chat in PHP” feel free to ask us by commenting below or visit on our contact page. Thank you.

Facebook Comments
(Visited 51 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here