user3297932
user3297932

Reputation: 31

how to replace abusive words php

hello all i have a page where i need to replace all the occurrence of some particular words which may be abusive with a image (same image for all the words) and i have following function which does the work but it is case sensitive . like it replaces f*k but not f*k f*k f*k f**k etc.. and i think there is a better way to do this please help .

my function is

function filter($text) {
    $icons = array(
            'f**k '    =>  '<img src="smiles/filter.png" class="filter" title="Blured text  "/>',
            'something else'    =>  '<img src="smiles/filter.png" class="filter" title="Blured text  "/>',
            ' something else'   =>  '<img src="smiles/filter.png" class="filter" title="f**k  you ::F"/>'
    );
    return strtr($text, $icons);
}

Upvotes: 1

Views: 140

Answers (1)

Torben
Torben

Reputation: 193

You'd need a bigger filter based on RegEx for this

Like this:

function filterAbusiveWords( $string ) {

    static $abusiveWords = array( 
        '/f[u]+c?k/i' => 'some icon',
        '/f[\*]+c?k/i' => 'some icon',
        '/f[a]+c?k/i' => 'some icon',
        '/some normal word/i' => 'some icon'
    );

    return preg_replace( array_keys( $abusiveWords ), array_values( $abusiveWords ), $string );
}

This would (in theory) replace all occurences of fak, fuck, f**ck, f*k, faaak, fuk etc.

Upvotes: 1

Related Questions