9999 Posted August 15, 2006 Share Posted August 15, 2006 This is a snipet of code I have. I eventually may want to increase this list to 10 words. What am I doing wong?[code]$search = $_GET['search'];$not_allowed = array('and', 'or', 'of', 'is', 'the');if (stristr($search, $not_allowed)) { echo 'Your search contains a word that is not allowed'; }[/code] Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/ Share on other sites More sharing options...
hostfreak Posted August 15, 2006 Share Posted August 15, 2006 Try this:[code]<?php$word_filter = array( 'and', 'or', 'of', 'is', 'the',);$search = $_GET['search'];function has_bad_words($search){ global $word_filter; $split = preg_split("#\s+#", $search, -1, PREG_SPLIT_NO_EMPTY); if (is_array($split)) { foreach ($split as $search_word) { if (in_array($search_word, $word_filter)) { return true; } } } return false;}if (has_bad_words($search)){ echo "Your search contains a word that is not allowed";}else{ echo "success";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-74921 Share on other sites More sharing options...
tomfmason Posted August 15, 2006 Share Posted August 15, 2006 or a simpler way is this.[code=php:0]$search = $_GET['search'];$list = array('and', 'or', 'of', 'is', 'the');foreach($list as $banned) { $match = strpos($search, $banned);}if ($match !== false) { echo "Your search contains banned words";}//continue your search[/code]Hope this helps,Tom Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-74923 Share on other sites More sharing options...
9999 Posted August 15, 2006 Author Share Posted August 15, 2006 Thanks guys for your help. I was still wondering if there is anything I can do with my existig code to make it work; particularly this line [code]if (stristr($search, $not_allowed))[/code] as I already have a complicated maze of nested loops and such. Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-74929 Share on other sites More sharing options...
9999 Posted August 15, 2006 Author Share Posted August 15, 2006 What about something like this:[code]if (in_array(stristr($search, $blocked)))[/code] Could this work with some modification? Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-74936 Share on other sites More sharing options...
9999 Posted August 15, 2006 Author Share Posted August 15, 2006 What would the correct syntax for the "if" line be? Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-75139 Share on other sites More sharing options...
tomfmason Posted August 15, 2006 Share Posted August 15, 2006 if you are meaning that last code that you posted, it would be like this.[code=php:0]if (in_array(stristr($search, $blocked))) { //echo something}else{ //do something else[/code] Link to comment https://forums.phpfreaks.com/topic/17585-using-an-array-to-restrict-search/#findComment-75300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.