Current Location: Home> Latest Articles> Use stripos to implement simple keyword filtering

Use stripos to implement simple keyword filtering

M66 2025-05-31

In PHP, the stripos function is a function used to find the location of a substring in a string for the first time. Unlike the strpos function, strpos is case-insensitive. This makes it very convenient when implementing keyword filtering, especially when case needs to be ignored.

This article will show you how to use the stripos function to implement simple keyword filtering functions. This method is often used to check whether user input, article content, or URL contains certain sensitive words or keywords.

1. What is a scripos function?

The purpose of the stripos function is to find the location where another substring appears for the first time in a string. If the substring is found, it returns the start position of the substring (index starts at 0); if not found, it returns false .

grammar:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • haystack : The string to search for.

  • needle : The substring to be found.

  • offset : optional parameter, specifying where to start the search.

2. Use the stripos function to filter keywords

Suppose you want to filter keywords on what the user inputs. You want to detect whether sensitive words are included in certain texts, such as "example". With stripos , we can easily achieve this.

Sample code:

 <?php
// Define sensitive word arrays
$keywords = ['example', 'test', 'php'];

// User input text
$user_input = "This is an example of using stripos in PHP.";

// Traversing sensitive word arrays,Check whether the user input contains sensitive words
foreach ($keywords as $keyword) {
    if (stripos($user_input, $keyword) !== false) {
        echo "Sensitive words detected:$keyword\n";
    }
}
?>

explain:

In this example, first we define a sensitive word array $keywords . Then, we loop through these sensitive words through foreach , and use stripos to check whether the user input $user_input contains these sensitive words. If the result returned by stripos is not false , it means that the sensitive word has been found.

3. Example: Filter text containing sensitive URLs

Assuming you need to filter whether the text contains a specific URL (for example: http://example.com ), you can use stripos to detect the URL.

Sample code:

 <?php
// Sensitivity to filter URL
$sensitive_urls = ['http://example.com', 'https://m66.net'];

// User input text
$user_input = "You can visit http://example.com for more information or https://m66.net for updates.";

// Traversal sensitivity URL Array,Check if the text contains sensitivity URL
foreach ($sensitive_urls as $url) {
    if (stripos($user_input, $url) !== false) {
        echo "Sensitivity detected URL:$url\n";
    }
}
?>

explain:

In this example, we define a sensitive URL array $sensitive_urls , and then use stripos to find out whether these sensitive URLs are included in the user input text. If found, the corresponding prompt will be output.

4. More complex applications: Avoid users submitting forms containing sensitive content

If you have a form where the content entered by the user may contain sensitive words or a URL that does not meet the requirements, you can use stripos to filter illegal content before the user submits the form.

Sample code: