$stringa = $_POST['nome'].' '.$citta.' '.$messaggio; function BadWords($stringa, array $paroleArray) { $detected = array(); foreach ($paroleArray as $parola) { if (preg_match("/\b{$parola}\b/i", $stringa, $matches)) { $detected[] = $matches[0]; } } return $detected; } $arr = BadWords($stringa, $paroleArray); foreach($arr as $valore) echo $valore;