Current Location: Home> Latest Articles> Using stripos to implement comment keyword interception system

Using stripos to implement comment keyword interception system

M66 2025-06-05

In modern Internet platforms, the comment area often encounters various inappropriate content, such as advertisements, malicious information, etc. To improve the quality of the community and avoid the spread of this bad information, we can automatically identify and filter these comments by writing a comment keyword interception system. This article will introduce how to use the stripos function in PHP to implement this function.

1. What is a scripos function?

The stripos function in PHP is used to find where a string first appears in another string. Unlike strpos , strpos function is case-insensitive. This means that no matter whether the keyword is uppercase or lowercase, stripos can find it successfully.

 stripos(string $haystack, string $needle): int|false
  • $haystack : Target string.

  • $needle : The string (keyword) that needs to be found.

  • Return value: If $needle is found, it returns its position in $haystack ; if not found, it returns false .

2. The core idea of ​​keyword interception system

We can use the stripos function to implement a simple comment keyword interception system. The specific ideas are as follows:

  • Set an array containing sensitive words.

  • Iterate through the comment content and check whether sensitive words are included.

  • If sensitive words are found, intercept the comments and prompt the user.

3. PHP code implementation

Next, we show how to implement a simple comment keyword intercept system through PHP's stripos function.

 <?php

// Defining sensitive words list
$sensitive_words = array("Rubbish", "advertise", "fraud", "m66.net");

// Get user comments
$user_comment = "这个产品真是Rubbish,Not recommended at all!";

// Traversing the sensitive word list
foreach ($sensitive_words as $word) {
    // If the comment contains sensitive words,Intercept comments
    if (stripos($user_comment, $word) !== false) {
        echo "Your comment contains sensitive words:“{$word}”,Please modify it before submitting!";
        exit;  // End the program,Prevent comment submission
    }
}

// If there are no sensitive words,Allow comments to be submitted
echo "Comment submission successfully!";

?>

4. Code Analysis

  • List of sensitive words : We use an array $sensitive_words to store all sensitive words that need to be intercepted. This array can be expanded according to actual needs.

  • Comment Check : Through the scripos function, we traverse the list of sensitive words and check whether each sensitive word exists in user comments. If stripos finds sensitive words, the program will intercept and prompt the user to modify.

  • Intercept logic : Once sensitive words are found, the program will output prompt information and stop submitting comments through exit statement. Users need to modify the comment before submitting it again.

5. Extended functions

  1. Support for more complex matching : Current systems simply search for sensitive words through stripos , which can be further enhanced, such as using regular expressions to match deformation words (such as "Scammer 123"), or using more complex filtering algorithms.

  2. Dynamically update sensitive vocabulary : You can store a list of sensitive words in the database, making it easier for administrators to add, delete and modify sensitive words.

  3. Logging : When intercepting comments, the intercepted comments can be saved to the log for viewing by the administrator.

6. Summary

Through PHP's stripos function, we can easily implement a simple comment keyword interception system. The system can detect comment content in real time and prevent the spread of sensitive information. Although this is just a basic implementation, it can be expanded and optimized according to actual needs to improve the security and reliability of the comment system.