note 36811 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 06 Jan 2008 12:49:43 +0000 |
| Subject: | note 36811 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134885@lists.php.net to get a copy of this message | ||
Note Submitter: thewolf at pixelcarnage dot com
----
I also wanted to replace rude words in a pice of text, I had str_replace replacing the word with
*** but then I had a thought, what if I could replace these bad words and words also very similar?
In the end I wrote this code, it uses two functions, one to check if the similarity between two
words is high enough to qualify for filtering out and one to do the actual replacing (modified from
my word_replace function):
<?php
$text = 'This is some text with a baword in it.';
echo similar_str_replace('badword', '*******', $text, 80);
function is_similar($one, $two, $similarity) {
similar_text($one, $two, $percent);
return $percent >= $similarity ? true : false;
}
/**
* Written by Rowan Lewis of PixelCarnage.com
* $search(string), the string to be searched for
* $replace(string), the string to replace $search
* $subject(string), the string to be searched in
* $similarity(int), how similar the two words must be
*/
function similar_str_replace($search, $replace, $subject, $similarity = 85) {
return preg_replace('/[a-zA-Z]+/e', 'is_similar(\'\0\', \'' .
$search . '\', \'' . $similarity . '\') ? \'' . $replace .
'\': \'\0\';', $subject);
}
?>
Just wack that into a php file, its ready for testing!
Hope someone uses it, and perhaps improves it somehow.