PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to require full (not partial) match with regex for a bad word filter?

 November 20, 2022     filter, php, preg-replace, profanity, regex     No comments   

Issue

I'm writing a very basic commenting system and want to implement a simple, efficient bad words filter.

I'm aware of the problems associated with bad word filters and realize it's basically impossible to write one that keeps misspellings and innuendo out, but I'm just wanting to write a very simple one that keeps correct spellings of vulgar words from being displayed.

I found a bad words list of about 400 words and put it into preg_replace() with the pattern being:

/(these|are|bad|words|like|ass)/

The problem is that it replaces any occurrence of the characters in the pattern, even if they are in the middle of a word. So, for example, assist will be replaced with ist.

Second question: instead of replacing the bad words with an empty string, or with a fixed-width string such as ****, is there a way to replace it with a string of asterisks with the same length of the replaced word?


Solution

preg_replace_callback(
    '/\b(these|are|bad|words|like|ass)\b/',
    function (array $match) { return str_repeat('*', strlen($match[1])); },
    $comment
)

\b is a word boundary and will probably suffice for most cases; though it probably won't be perfect for all cases.



Answered By - deceze
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing