Replace Bad word in array php

Hello Guys i want to replace bad word to something like this F*ck
i need fast way to do that.

For now i am using this cods its work but i need improved one

  $swears = array(
    "fuck" => "f**k",
      "fUck" => "f**k",
        "fuCk" => "f**k",
          "fucK" => "f**k",
    "suck"  => "s**k",
    "fellow" => "fe**ow"
);

str_replace(array_keys($swears), array_values($swears), $string);

You can see i was convert one word to another using many rows i need faster codes to do that.

You could make it easier with str_ireplace in stead of str_replace

1 Like

how much faster in milliseconds? and how comes this is the bottleneck of your entire application?

i want to replace bad words

Yes, I gathered that.
Using str_ireplace you would not need all the variations of capitals and lower case, which would slim down your code. You would just need an array of words (regardless of case) and another of replacements.

$find = array('feck', 'suck', 'follow');
$replace = array('f**k', 's**k', 'fo**ow');
$clean = str_ireplace($find, $replace, $string);

Or there are tools to help with this task.

If you see the thread that comes from, there can be other complications to this.

People are always going to be able to get around a swear word filter no matter hard you try.

2 Likes

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.