tags:

views:

35

answers:

2

Hi, I am trying to basically create a list of obscene words for a filter. I wanted to generate the list by creating arrays of characters and their replacements, example. 'A' can be replaced with '4', or "E" with "3".

So basically I have a bunch of arrays for every char in the alphabet with the several different ways to replace it. EG. $e = array( "e" =>"3"); I have an array of obscene words. I need to print all the obscene words and then their variants where the letters match. Example:

Hello He11o He1lo H3llo H31lo H311o

Every variation. How would I go about doing this? Any help would be much appreciated.

A: 

Sounds like a job for regular expressions for me.

preg_replace('/H[e3][l1]{2}[0o]/i','H****',$textstr);
Weston C
A: 

Basically what this code does is loops through each obscene word in the $arr_obscene array, splits it by every character into a temporary array called $tmpand then for every character, it check if an element with a matching key exists in the $arr_replaces array. If so, it replaces it, echoes the resulting string and continues with the next character.

It's not perfect since it forgets about some of the combinations but it's a start, an approach.

$arr_replaces = array('e' => '3', 'l' => '1', 'o' => '0');
$arr_obscene  = array('Hello','World');

foreach($arr_obscene as $obscene){
    $tmp = preg_split('//', $obscene, -1, PREG_SPLIT_NO_EMPTY);
    foreach($tmp as $character){
        if(array_key_exists($character, $arr_replaces)){
        $obscene = substr_replace($obscene,
                                  $arr_replaces[$character],
                                  strpos($obscene,$character),
                                  1);
            echo $obscene."<br/>";
        }
    }
    unset($tmp);
}

//Would output:
H3llo
H31lo
H311o
H3110
W0rld
W0r1d
thisMayhem