note 58317 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:04:57 +0000 |
| Subject: | note 58317 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137420@lists.php.net to get a copy of this message | ||
Note Submitter: admin at sellchain dot com
----
<?
# This is a complete profanity filter. I couldn't get any of the preg_replace statements
working, so I made one that is CASE INSENSITIVE, SPACE INSENSITIVE too.
$original="This contains curseword, curse_word1, cursewor_d2, cursewo_rd3, and a bunch of other
text...";
echo "Before: $original<br><br>";
$original=RemoveCurseWords($original);
echo "After: $original<br><br>";
function RemoveCurseWords($original) {
$patterns[0] = 'curseword';
$patterns[1] = 'curse_word1';
$patterns[2] = 'cursewor_d2';
$patterns[3] = 'cursewo_rd3';
$finalremove=$original;
$piece_front="";
$piece_back="";
$piece_replace="***";
for ($x=0; $x < count($patterns); $x++) {
$safety=0;
# Prevents infinity loops if you accidentally put in replacements that match your patterns. it could
happen! :)
# The saftey could cause issues if you are replacing more than 100,000 occurrences in a string, so
remove it if you trust it.
while(strstr(strtolower($finalremove),strtolower($patterns[$x]))) {
# find & remove all occurrence
$safety=$safety+1;
if ($safety >= 100000) { break; }
$occ=strpos(strtolower($finalremove),strtolower($patterns[$x]));
$piece_front=substr($finalremove,0,$occ);
$piece_back=substr($finalremove,($occ+strlen($patterns[$x])));
$finalremove=$piece_front . $piece_replace . $piece_back;
} # while
} # for
return $finalremove;
}# function RemoveCurseWords