When developing a website or system, we often need to judge whether the title of the article contains certain sensitive words, especially to maintain the health of the content. PHP provides many string processing functions, among which the stripos function can be used very conveniently to find whether a string contains a specific substring. In this article, we will explain how to use the scripos function to match an array to determine whether the title of the article contains sensitive words.
The stripos function is a function in PHP that finds the position of a string in another string. Unlike strpos , strpos is case-insensitive, so it is more suitable for checking for sensitive words that do not take into account case.
stripos($haystack, $needle);
$haystack : The string to search for (such as the article title).
$needle : The substring to be found (such as sensitive words).
If a sensitive word is found, stripos returns the first occurrence position (counting from 0), and if not found, false .
Suppose we have a list of sensitive words, how do we determine whether the title of the article contains these sensitive words? We can store sensitive words in an array and then loop through the array using stripos to find out if the title contains these words.
<?php
// List of sensitive words
$sensitive_words = ["pornography", "Violence", "gamble", "poison"];
// Article title
$title = "这是一个关于gamble的文章";
// Traversing sensitive word arrays,Check whether the title contains sensitive words
foreach ($sensitive_words as $word) {
if (stripos($title, $word) !== false) {
echo "Title contains sensitive words: $word\n";
}
}
?>
In this example, we define a sensitive word array $sensitive_words , and then iterate through this array. For each sensitive word, we use stripos to check if it appears in the article title. If the result returned by stripos is not false , it means that the sensitive word has been found.
Once sensitive words are detected, we can take some measures, such as displaying warning messages, prohibiting posting of articles, etc. Here is a more complete example showing how to find sensitive words in the title of the article and give warnings:
<?php
// List of sensitive words
$sensitive_words = ["pornography", "Violence", "gamble", "poison"];
// Article title
$title = "关于poison的非法交易";
// Whether the variable tag contains sensitive words
$contains_sensitive_word = false;
// Traversing sensitive word arrays,Check whether the title contains sensitive words
foreach ($sensitive_words as $word) {
if (stripos($title, $word) !== false) {
$contains_sensitive_word = true;
echo "Title contains sensitive words: $word\n";
}
}
// 如果Title contains sensitive words,Return warning message
if ($contains_sensitive_word) {
echo "warn:The title contains inappropriate content,Please modify it before submitting。\n";
} else {
echo "Legal title,Can continue to publish。\n";
}
?>
In this example, we added a $contains_sensitive_word variable to mark whether the title of the article contains sensitive words. If sensitive words are included, we will give a warning asking the user to modify the title.
Assuming that we need to replace the URL domain name in the article we are dealing with, we can use the str_replace function to replace all URL domain names from the original domain name to m66.net . For example: