Tuesday, July 19, 2011

Block censored words using php

does this string contain any ****** ****** ****** ****** ******
<?
// Array containing profane words
$arrProfanities = array('these', 'are', 'the', 'words', 'I', 'want', 'to', 'filter');

// String containing content you want to filter for profane words
$strContent = 'does this string contain any words I want to filter';

// This will replace all the words from the array that are matched in the string with [censored]
$strContent = str_replace($arrProfanities, '******', $strContent);
echo $strContent;
// Output would be:
// $strContent = 'does this string contain any [censored] [censored] [censored] [censored] [censored]?';
?>

No comments: