Jump to content


Photo

Using an array to restrict search


  • Please log in to reply
6 replies to this topic

#1 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 15 August 2006 - 04:46 AM

This is a snipet of code I have.  I eventually may want to increase this list to 10 words.  What am  I doing wong?

$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';
        }


#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 15 August 2006 - 05:14 AM

Try this:
<?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";
}
?>


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 15 August 2006 - 05:21 AM

or a simpler way is this.

$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

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 15 August 2006 - 05:52 AM

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
if (stristr($search, $not_allowed))
as I already have a complicated maze of nested loops and such.

#5 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 15 August 2006 - 06:04 AM

What about something like this:
if (in_array(stristr($search, $blocked)))
Could this work with some modification?

#6 9999

9999
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 15 August 2006 - 02:50 PM

What would the correct syntax for the "if" line be?

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 15 August 2006 - 08:07 PM

if you are meaning that last code that you posted, it would be like this.

if (in_array(stristr($search, $blocked))) {
   //echo something
}else{
   //do something else


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users