PHP: Swear Word Filter


The code used for index.php is below:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../styles.css">
  <title>PHP: Swear Word Filter</title>
</head>
<body>
  <h1 class="lesson-h1">PHP: Swear Word Filter</h1>

<?php

include("../connection.php");
$string = "Bloody fucking bitch.";

function censor($string){
  if($string){
    $swear_words = ["fuck", "shit", "bitch", "cunt", "whore", "cock", "dick", "penis", "pussy", "tit", "ass", "ho", "boob", "twat", "damn"];

    $replacements = ["f**k", "sh*t", "b*tch", "c*nt", "wh**e", "c*ck", "d*ck", "p*nis", "pu**y", "t*t", "a**", "h*", "b**b", "tw*t", "d*mn"];

    $new_string = strtolower(str_replace($swear_words, $replacements, $string));
    // echo "p".$new_string."</p>";
    return $new_string;
  } else {
return "Error: Text Field is Empty";
    }
} 

$user_text = htmlentities($_POST['text']);

if(isset($_POST['submit'])){
    $censored_text = censor($user_text);
    // echo $censored_text;
}

?>

<form action="index.php" method="post">
    <textarea name="text" id="text" cols="30" rows="10" placeholder="Paste text here"></textarea>
    <input name="submit" type="submit" value="Submit">
</form>

<h3><?php echo $censored_text; ?></h3>
</body>
</html>
<?php
  include('../show_code.php');
  show_code('index.php');
?>