-1

I have in php an array of banned words.

With this code I replace the banned words with * in a string:

foreach ($wordlist as $word)
if (stripos($str, $word) !== false)
$str = str_ireplace($word, str_repeat('-*', strlen($word)), $str);
return $str;

The problem is that some users add spaces within words, so the code does not find them.

Example:
Banned word: apple
If I write apple is replaced with *****
If I write ap ple isn't replaced

Is there a way to use str_ireplace ignoring the white space?

Cristik
  • 30,989
  • 25
  • 91
  • 127
Cristian Livella
  • 45
  • 1
  • 3
  • 8
  • there are a few ways to get rid of the spaces from user input, sure. Did you try and find something first? – Funk Forty Niner May 24 '16 at 16:01
  • 1
    possible duplicate of [To strip whitespaces inside a variable in PHP](http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php) – Funk Forty Niner May 24 '16 at 16:02
  • 2
    This is extremely hard to do, if i want to tell the world about my love of app1es, a p p l e s, @pples, ápples (you get the picture) you cant expect to stop me via code alone. Community moderation is probably the best solution – Steve May 24 '16 at 16:08
  • @Fred-ii- I do not want remove the spaces. At the end have to stay, I just want to be ignored by str_replace – Cristian Livella May 24 '16 at 16:10
  • @Steve I can not use community moderation because it is a chat. I know it is impossible to prevent the use of a word, but ignoring the spaces definitely it will be less used. – Cristian Livella May 24 '16 at 16:13
  • Freedom of expression. By having an open board for people, then it's just what it is; "open". I agree with @Steve on this one. However, there are scripts out there that will let you accept a post before it goes in. I know it could mean a lot of time to monitor, but that's the reality of it Cristian and you'll need to live with it and the consequences. – Funk Forty Niner May 24 '16 at 16:16
  • @CristianLivella That's almost machine learning If you want to find out all combination of `apple` automatically with or without spaces and that's just one word – Hanky Panky May 24 '16 at 16:17
  • I imagine if you're dead set on this, you can use regex and have some sort of optional white space match between each letter on the word. Although as other [posts](http://stackoverflow.com/questions/24515/bad-words-filter) have said, it isn't a full proof or advised solution `(a(\s)*?p(\s)*?p(\s)*?l(\s)*?e)` is a bad, and not really maintainable example but it would possibly work for just spaces but I could easily put app1e and you're back to square one. – Mikey May 24 '16 at 16:17
  • Anyone that uses a space in the word is deliberately trying to subvert the censorship. They will try again, and succeed. The more you retaliate (by adding more and more 'bad' words), the higher the chance you penalize legitimate users "Have you used the android **app les** dawsons blankety blank?" gets censored – Steve May 24 '16 at 16:25

1 Answers1

0

This is what I'd probably do:

foreach ($wordlist as $word) {
     //Convert each word to a regex which matches containing spaces (e.g. apple => a\s*p\s*p\s*l\s*e)
     $regexWord = implode("\s*?",str_split($word)); 
     $str = preg_replace("/".$regexWord."/",str_repeat('-*', strlen($word)), $str);

}

The idea is to covert each word into a regex which matches the word even if it contains spaces. This may or may not be a good idea, but it's what you seem to need achieve.

You can control the characters between your word with implode("[list characters]*",str_split($word))

apokryfos
  • 38,771
  • 9
  • 70
  • 114