<?php

$badwords = array('shit', 'fuck'); // Here we can use all bad words from database

$text = 'Man, I shot this f*ck, sh/t! fucking fu*ker sh!t f*cking  sh\t ;)';

echo "filtered words <br>";

echo $text."<br/>";

$words = explode(' ', $text);

 

foreach ($words as $word)

	{

		$bad= false;

		foreach ($badwords as $badword)

			{

				if (strlen($word) >= strlen($badword))

				{

					$wordOk = false;

					for ($i = 0; $i < strlen($badword); $i++)

					{	

						if ($badword[$i] !== $word[$i] && ctype_alpha($word[$i]))

						{

							$wordOk = true;

							break;

						}

					}

					if (!$wordOk)

					{

						$bad= true;

						break;

					}

				}

			}	

			echo $bad ? 'beep ' : ($word . ' '); // Here $bad words can be returned and replace with *. 

	}