Return to Snippet

Revision: 6397
at May 19, 2008 18:32 by oso96_2000


Updated Code
<?php
function filtrado($texto, $reemplazo = false)
{
	$filtradas = 'fu?ck, shit'; //Define here your words to censor separated by comma (sorry for the badwords, it's just an example)

	$f = explode(',', $filtradas);
	$f = array_map('trim', $f);
	$filtro = implode('|', $f);

	return ($reemplazo) ? preg_replace("#$filtro#i", $reemplazo, $texto) : preg_match("#$filtro#i", $texto) ;
}
?>

Revision: 6396
at May 19, 2008 16:39 by oso96_2000


Initial Code
<?php
function filtrado($texto, $reemplazo = false)
{
	$filtradas = 'fu?ck'; //Define here your word to censor

	$f = explode(',', $filtradas);
	$f = array_map('trim', $f);
	$filtro = implode('|', $f);

	return ($reemplazo) ? preg_replace("#$filtro#i", $reemplazo, $texto) : preg_match("#$filtro#i", $texto) ;
}
?>

Initial URL
http://osiux.ws/2008/04/29/filtro-de-palabras-en-php/

Initial Description
This function uses the power of regexp to check if some bad word are on the text, also offers the posibility to change those word for something else. Examples:\r\n\r\n$texto = \'fuck off!\';\r\nfiltrado($texto); returns true since a bad word has been found on the text\r\nfiltrado($texto, \'[censored]\'); //returns [censored] off!\r\n\r\nAnd because regexp, this will work with something like \"fck off!\". You can see a more detailed example here:\r\nhttp://www.otaku-anime.com/varios/filtro.php -- Example\r\nhttp://www.otaku-anime.com/varios/filtro.php?source -- Source code of the example

Initial Title
Censor bad words with regexp

Initial Tags
regex, replace

Initial Language
PHP