Current Location: Home> Latest Articles> How to combine stripos and str_ireplace to achieve sensitive word replacement

How to combine stripos and str_ireplace to achieve sensitive word replacement

M66 2025-05-31

In PHP, handling sensitive word substitution is a common requirement, especially on social platforms, forums and other websites. In order to ensure that the user's language complies with regulations, we need to accurately replace sensitive words. stripos and str_ireplace are two commonly used PHP functions that work well together to achieve this.

Basic ideas

stripos is a function used to find the first occurrence of a substring, it is case-insensitive; str_ireplace is a function that can be used to replace a string, it is also case-insensitive. We can find the position of the sensitive word through stripos and then replace it with str_ireplace .

1. Stripos function

The function of the stripos function is to find the position information of the first occurrence of a string and return the position of the first occurrence of a substring in the string. This function is case-insensitive.

grammar:

 stripos($haystack, $needle, $offset);
  • $haystack : The string to be found.

  • $needle : The substring to be found.

  • $offset : optional, specify where to start searching.

Example:

 $haystack = "Hello world!";
$needle = "world";
$position = stripos($haystack, $needle);
echo $position; // Output 6

2. str_ireplace function

str_ireplace is a function used to replace strings, and is case-insensitive.

grammar:

 str_ireplace($search, $replace, $subject, &$count);
  • $search : The string to be found.

  • $replace : The string used to replace.

  • $subject : The target string to be replaced.

  • $count : optional, returns the number of replacements.

Example:

 $subject = "Hello world!";
$search = "world";
$replace = "PHP";
$result = str_ireplace($search, $replace, $subject);
echo $result; // Output Hello PHP!

3. How to use stripos and str_ireplace together

To achieve accurate replacement of sensitive words, we can combine the functions of stripos and str_ireplace . Stripos is used to find the position of sensitive words. If the sensitive words exist, then replace them with str_ireplace .

Sample code:

 <?php
// Defining sensitive words list
$sensitiveWords = ['badword', 'offensiveword', 'curseword'];

// Entered string
$text = "This is a test string containing badword and offensiveword.";

// Traversing the sensitive word list,Replace one by one
foreach ($sensitiveWords as $word) {
    // use stripos Determine whether sensitive words are included
    if (stripos($text, $word) !== false) {
        // use str_ireplace Replace sensitive words
        $text = str_ireplace($word, '***', $text);
    }
}

echo $text;  // Output:This is a test string containing *** and ***.
?>

4. Process sensitive words in URLs

In many cases, the text entered by the user may contain a URL. If there are sensitive words in these URLs, we need to process them as well. Using stripos and str_ireplace to easily replace sensitive words in URLs. For example, if a sensitive word appears in the URL, we can replace the domain name with m66.net to ensure that the URL entered by the user is safe.