0

I want a function or method in PHP that will detect swearwords in obscured text. Something that will check string like:

$string = "hey you swearword!" or 
$string = "hey you swear#word!"

or maybe even

$string = "hey you sw3arw0rd!"

for "swearword" and will return true if it contains that bad swearword and false if it does not. I don't want people to use bad word on my site, please help!

youmound
  • 53
  • 4
  • 1
    That function didnt exists, but you can make it on your own, helpfull php functions are `similar_text`, `soundex`, `levenshtein` and you can always block a word that have special chars in it see `strpos`, but the hole topic is lot of work to do – JustOnUnderMillions Jul 27 '16 at 13:46
  • Can I plase example function validate ? – youmound Jul 27 '16 at 13:48
  • Have a read of this: http://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter – Carl Casbolt Jul 27 '16 at 13:48
  • 1
    https://blog.codinghorror.com/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea/ relevant – Athena Jul 27 '16 at 14:18
  • Most profanity filters are a waste of time because people will always work out a way around them. Those that do successfully filter out bad content almost always have a major problem with false positives. You are liable to end up with [the Clbuttic mistake](https://what.thedailywtf.com/topic/2302/the-clbuttic-mistake) or the [Scunthorpe problem](https://en.wikipedia.org/wiki/Scunthorpe_problem). If you have a forum that absolutely *requires* this kind of filtering, then you probably need active moderation anyway, because there are plenty of ways of being obscene without swearing at all. – Simba Jul 27 '16 at 16:08

1 Answers1

0

Just a simple example to show the direction:

$stopwords = ['swearword'];

$test = ['swear#word','sw3arw0rd','goodword','swearw*rd','swe*rw*rd','swe*!**rd'];

foreach($test as $word){
    foreach($stopwords as $stopword){
        if(levenshtein($stopword,$word)<=2){
            print "levenshtein: '$word' seems to mean $stopword<br/>";
            continue 2;
        }
    }
    if(strlen(preg_replace('#[a-zA-Z]+#','',$word))!==0){#special char found
        print "preg_replace: '$word' seems to have illegal chars<br/>";
        continue;
    }
    print "'$word' seems be NO stopword<br/>";
}
JustOnUnderMillions
  • 3,741
  • 9
  • 12