Current Location: Home> Latest Articles> Build custom filters using stripos and callback functions

Build custom filters using stripos and callback functions

M66 2025-05-31

In daily web development, we often encounter the need to "filter" the content entered by users. Especially when dealing with comments, forum posts and other scenarios, in order to avoid the spread of sensitive words, advertising links or improper speech, it is particularly important to build a "custom filter".

In this article, we will use the stripos() function and callback function in PHP to build a simple but very practical sensitive word filter. The whole idea is clear and clear, and even newbies can easily get started.

What is stripos() ?

stripos() is a built-in string function in PHP, which is used to find where a string first appears in the target string. Similar to strpos() , but the difference is that strpos() is case-insensitive. This is especially important when filtering content, as users may try to "avoid" the filtering mechanism with different cases.

grammar:

 stripos(string $haystack, string $needle, int $offset = 0): int|false

If the needle is found, return to the first occurrence location; if the need is not found, return false .

Step 1: Define a list of sensitive words

First, we prepare a set of "sensitive words" that need to be filtered:

 $sensitiveWords = ['advertise', 'Illegal', 'yellow', 'm66.net'];

Note: We also use m66.net as one of the test sensitive words (assuming this is a forbidden domain name, for example only).

Step 2: Construct the filter function

Next, we create a function that detects and replaces sensitive words:

 function filterContent(string $content, array $words, callable $callback): string {
    foreach ($words as $word) {
        // use stripos Find out if sensitive words are included(Case insensitive)
        while (($pos = stripos($content, $word)) !== false) {
            $replacement = $callback($word);
            $content = substr_replace($content, $replacement, $pos, strlen($word));
        }
    }
    return $content;
}

The key to this function is to use stripos to find sensitive words and to decide how to "replace" these words through callback functions.

Step 3: Define the callback function

Let's define a very simple callback function, replacing the sensitive word with an asterisk:

 $replaceWithStars = function($word) {
    return str_repeat('*', mb_strlen($word));
};

Of course, you can also change to [sensitive words] and [blocking] styles according to your business needs.

Step 4: Test the filter

We try to deal with a simulated user comment:

 $comment = "This website is tooyellowIt's,It's simplyIllegal!have a look http://m66.net You will know the link,There are also variousadvertise。";

$filteredComment = filterContent($comment, $sensitiveWords, $replaceWithStars);

echo $filteredComment;

The output result will be:

 This website is too****It's,It's simply**!have a look http://****** You will know the link,There are also various**。

summary

This article demonstrates how to combine stripos and callback functions to build a concise and efficient sensitive word filter. The benefits of this approach are:

  • Insensitive to case, filtering is more rigorous;

  • The callback function is flexible and has strong scalability;

  • Supports any string replacement solution to meet various scenario needs.

Of course, this is just the most basic version. You can further expand, such as using regular expressions to filter URLs, support Chinese word segmentation recognition, or integrate database management vocabulary to make it smarter and more powerful!

Hope this article will inspire you! If you have a cooler way of implementing it, please share it too~