3v4l.org

run code in 300+ PHP versions simultaneously
<?php function badwords_full($string, &$bad_references) { static $bad_counter; static $bad_list; static $bad_list_q; if(!isset($bad_counter)) { $bad_counter = 0; $bad_list = badwords_list(); $bad_list_q = array_map('preg_quote', $bad_list); } return preg_replace_callback('~('.implode('|', $bad_list_q).')~', function($matches) use (&$bad_counter, &$bad_references) { $bad_counter++; $bad_references[$bad_counter] = $matches[0]; return mb_substr($matches[0], 0, 1).str_repeat('&squf;', strlen($matches[0]) - 1); }, $string); } function badwords_list() { # spanish $es = array( "gallina", "ñoño" ); # english $en = array( "chicken", "horse" ); # join all languages $list = array_merge($es, $en); usort($list, function($a,$b) { return strlen($b) < strlen($b); }); return $list; } echo badwords_full('Hola en español eres un ñoño!', $bad);

preferences:
25.1 ms | 411 KiB | 5 Q