<?php $badWords = array('Note', 'bush'); function replacebadwords($string,$badword_array) { foreach ($badword_array as $v) { $newstring = ""; for($i = 0; $i < strlen($v); $i++) { $newstring .= "*"; } $string = str_replace($v,$newstring,$string); } return $string; } $text = "Note mom is george bush's grandma."; $text = replacebadwords($text,$badWords); echo $text; ?>