In the form submission, users may sometimes enter some inappropriate text or sensitive information. In order to ensure the health and security of platform content, we can use PHP's stripos function to search and filter user input. This article will introduce how to use the stripos function to implement keyword search and filtering in form content review, and give examples.
stripos is a function in PHP that is used to find the first occurrence of a substring in a string. The function is case-insensitive, and the return value is the location where the substring first appears, and if not found, it returns false . This function is very useful when doing sensitive information or keyword searches.
stripos($haystack, $needle, $offset);
$haystack : The string to search for.
$needle : The substring to be found.
$offset : (Optional) If an offset is set, the search starts from that location.
During the review of form content, we can use stripos to find out whether there are certain sensitive keywords. For example, suppose we are looking for whether the form submitted by the user contains certain sensitive words, and if these words are found, we can refuse to submit or prompt the user.
Here is a simple example that demonstrates how to use the scripos function to detect if the content of a user submitted form contains inappropriate vocabulary.
<?php
// Defining sensitive words list
$sensitiveWords = ['Sensitive words1', 'Sensitive words2', 'Inappropriate content'];
// Simulate form input content
$formInput = $_POST['user_input'] ?? '';
// 检查表单输入是否包含Sensitive words
foreach ($sensitiveWords as $word) {
if (stripos($formInput, $word) !== false) {
echo "warn:您的输入包含Inappropriate content,Please modify it before submitting。";
// Here,You can choose to reject form submission or perform other processing
exit;
}
}
// Continue to process form data
echo "Your form has been submitted successfully!";
?>
In the above code, we define a list of sensitive words $sensitiveWords and check whether the form content entered by the user contains these sensitive words one by one through the scripos function. If a match is found, a warning message is output and processing is terminated. If no sensitive words are found, the form will be submitted normally.
Sometimes, the form content may contain a URL link. We can also use stripos to check whether the URL contains inappropriate domain names or sensitive links. Here is an example to check whether the form content contains certain unauthorized URLs.
<?php
// Define a list of unauthorized domain names
$disallowedDomains = ['example.com', 'anotherexample.com'];
// Simulate form input content
$formInput = $_POST['user_input'] ?? '';
// FindURLDomain name in
preg_match_all('/https?:\/\/([^\/]+)/', $formInput, $matches);
// Iterate through all matching domain names
foreach ($matches[1] as $domain) {
if (in_array($domain, $disallowedDomains)) {
echo "warn:Your input contains links that are not allowed({$domain})。Please modify it before submitting。";
exit;
}
}
// Continue to process form data
echo "Your form has been submitted successfully!";
?>
In this example, we use preg_match_all to extract the domain names in all URLs, and then use in_array to check whether there are unauthorized domain names. You can modify the list of unauthorized domain names according to actual needs.
Performance issues : Frequent use of stripos may affect performance when the form content is long or the list of sensitive words is very large. At this time, you can consider using regular expressions or other optimization strategies.
Incomplete match : stripos is based on string searches, and there may be partial matches. To avoid misjudgment, sensitive words can be added before and after spaces or other special characters for isolation.
Enhanced user experience : If you want instant feedback when user input, you can consider combining JavaScript and AJAX for front-end detection to improve the user experience.