Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Preg_replace whole word only
Old 03-12-2010, 01:49 PM Preg_replace whole word only
Extreme Talker

Posts: 199
Trades: 0
Im trying to make a naughty word filter. It removes bad words fine, but instances where there is a bad word found in the text like "assist" and "asses" get caught in the filter as well. Strangely though if the sentence is: My asses to assist me." the clean version will read: My asses to ***ist me." It seems to clear the first use of the word in another word, but then blocks the rest. Any ideas? My script is below. Thanks.

Code:
function cleanWords($value) {

    /*   strip naughty words   */
    $bad_word_file = 'standards/badwords.txt';
    $strtofile = fopen($bad_word_file, "r");
    $badwords = explode("\n", fread($strtofile, filesize($bad_word_file)));
    fclose($strtofile);
    
    for ($i = 0; $i < count($badwords); $i++) {
        $wordlist .= str_replace(chr(13),'',$badwords[$i]).'|';
    }
    $wordlist = substr($wordlist,0,-1);

    $value = preg_replace("/\b($wordlist)\b/ie", 'preg_replace("/./","*","\\1")', $value);    
    return $value;

}
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-12-2010, 06:52 PM Re: Preg_replace whole word only
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Since your replacing by word bounderies, then the only why it would catch that is if the word asses was in the badwords.txt
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Preg_replace whole word only
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16243 seconds with 12 queries