Jump to content

Swear Filter Question (class)


Canman2005

Recommended Posts

Hi all

 

I've done a function to replace swear words.

 

The problem I have is if someone types "class" then it returns "cl***" thinking the last part of "class" (ie: "ass") is a swear word. I had to therefore remove it.

 

Is there a separate replace function I can run just to check for the single word "ass" and then replace it?

 

Thanks in advance

Ed

Link to comment
Share on other sites

You can break the string into an array of words, and match each word (case insensitive) to the list of profanities.

 

 

 

long process what if you have say: teng, astig , ako and you have to find them so the process will need loop and if condition right?

Link to comment
Share on other sites

long process what if you have say: teng, astig , ako and you have to find them so the process will need loop and if condition right?

 

Yes it will be a longer code vs the user of regex, but the it will run faster.  I see nothing wrong with writing code using loop and condition.

Link to comment
Share on other sites

<?php
$data = "hello you bad1, how the bad2";
$Bad= array("bad1", "bad2", "bad3");
$Replace   = array("***", "***", "***");
$data = str_ireplace($Bad, $Replace, $data);
?>

 

If you want advanced (more flexable) filtering then RegEx would be better, but a little slower

RegEx would probably be my choice, as you can catch things which would noramlly require a few lines use any other method

simple RegEx Example

<?php
$data = 'hello you bad1, how the bad2';
$patterns[] = '/bad1/i';
$replacements[] = '***';

$patterns[] = '/bad2/i';
$replacements[] = '***';

$patterns[] = '/f??k/i';
$replacements[] = '***';

$data =  preg_replace($patterns, $replacements, $data);
?> 

 

You can break the string into an array of words, and match each word (case insensitive) to the list of profanities.

That will be more complex and much slower..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.