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
https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/
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?

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.

<?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..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.