Find occurences of multiple needles in haystack
Let’s say you got a form and you don’t want to allow that people submit certain words. How would you do that? Right you will have to use the PHP function strpos. Unfortunately you can’t feed strpos with arrays (a list of needles)!
Here is a very simple solution that you can use to to check multiple needles and their occurence in a specific string.
$haystack = $myhaystack; $needle = array('badword','badword','badword','badword'); foreach($needle as $value){ $pos = strpos($haystack, $value); if ($pos !== false){ /* If we find one of those bad words do the following */ header("Location: http://mysite.com?error=badcontent"); exit; } }
I used the “header” command to redirect people to a specific site if they enter those bad words. You can output a customized error message by adding a variable at the end of the URL e.g. the variable “error”. Simply retrieve the variable via GET and then echo an appropriate message.
If you have any question feel free to ask!
Enjoy
Like our posts? Then subscribe via Mail:
Similar Posts:
- Fixing Popularity Contest
- Free Online Article Rewriter
- child pid exit signal segmentation fault 11
- Premature end of script headers in index.php + 500 Error
- Apache 403 Forbidden Linux – Solution
Socialize:
|
|











Leave your response!