Jump to content

[SOLVED] Help Simplifying an If Statement


limitphp

Recommended Posts

I want to check to see if a $string has the following:

a space, or, the, and, a

 

If it has any of the above, I want to echo "not valid".

 

I want to make sure it only catches the word and not those words within a word....

in other words, "or" is not valid, but "for" would be.

 

Would a strstr be the best thing to use?

 

can someone help me construct it?

I know this probably isn't right:

 

ex)

$needle = array("and", "or", " ", "the");

 

if (strstr($string, $needle) === false {

  echo "not valid"

}

 

Link to comment
Share on other sites

If you are doing this in preparation for a mysql text search, you should consider Mysql's built in stop words stuff.

 

http://dev.mysql.com/doc/refman/5.1/en/fulltext-stopwords.html

whoa...it stops way too many words.

I can understand if I was creating a search engine...but my search feature is searching only band names and song names.  I want most of those words to be searchable.

Link to comment
Share on other sites

http://dev.mysql.com/doc/refman/5.1/en/fulltext-fine-tuning.html

 

To override the default stopword list, set the ft_stopword_file system variable. (See Section 5.1.3, “Server System Variables”.) The variable value should be the pathname of the file containing the stopword list, or the empty string to disable stopword filtering. After changing the value of this variable or the contents of the stopword file, restart the server and rebuild your FULLTEXT indexes.

 

However, this may be overkill for what you are doing.

Link to comment
Share on other sites

http://dev.mysql.com/doc/refman/5.1/en/fulltext-fine-tuning.html

 

To override the default stopword list, set the ft_stopword_file system variable. (See Section 5.1.3, “Server System Variables”.) The variable value should be the pathname of the file containing the stopword list, or the empty string to disable stopword filtering. After changing the value of this variable or the contents of the stopword file, restart the server and rebuild your FULLTEXT indexes.

 

However, this may be overkill for what you are doing.

 

it might be a good idea if I ever need to expand on the list or do a more complex search.

To use the stop word list, would I just use a regular query?  Also, would I have to index the table?  If so, what does indexing a table mean?

Link to comment
Share on other sites

Maybe this will help a little bit more, it will return a list of good values in the search, and a list of the bad ones. Alter as needed.

 

<?php
function checkValid($string)
{
$bad = array('or', 'the', 'and', 'a');
$newString = explode(" ", strtolower($string));
for ($x = 0; $x < sizeof($newString); $x++)
{
	$isGood = in_array($newString[$x], $bad);
	if ($isGood == false)	
		$goodString[] = $newString[$x];
	else
		$badString[] = $newString[$x];		
}
$endRet[] = $goodString;
$endRet[] = $badString;
return $endRet;
}
?>

 

If you return the function into variables $result, $result[0] would be an array with the good values, and $result[1] would be an array with the bad values.

Link to comment
Share on other sites

Maybe this will help a little bit more, it will return a list of good values in the search, and a list of the bad ones. Alter as needed.

 

<?php
function checkValid($string)
{
$bad = array('or', 'the', 'and', 'a');
$newString = explode(" ", strtolower($string));
for ($x = 0; $x < sizeof($newString); $x++)
{
	$isGood = in_array($newString[$x], $bad);
	if ($isGood == false)	
		$goodString[] = $newString[$x];
	else
		$badString[] = $newString[$x];		
}
$endRet[] = $goodString;
$endRet[] = $badString;
return $endRet;
}
?>

 

If you return the function into variables $result, $result[0] would be an array with the good values, and $result[1] would be an array with the bad values.

on the $newString = explode(" ", strtolower($string));

 

if they only type one word, and you explode where there is a space....will it still be able to handle one word?

 

I think thats where i was confused....

I originally had this (thanks to premiso):

<?php
function parse_query($string, $col) {
if (strstr($strng, " ") === false)
	return " $col LIKE '%" . check_input($string) . "%' ";

   $words = explode(" ", strtolower($string));
   $statement = array();
   $notAllowed = array("and", "the", "or"); // add more if you like
   foreach ($words as $word) {
	 if (strlen($word) > 2 && !in_array($word, $notAllowed)) {
		 $statement[] =  " $col LIKE '%" . check_input($word) . "%' ";
	 }
   }

   if (count($statement) > 0)
	return implode(" OR ", $statement);
   else
	return false;  
}

 

And i kept looking at the first part:

if (strstr($strng, " ") === false)

return " $col LIKE '%" . check_input($string) . "%' ";

 

and I was thinking...is that checking to see if it only has one word....if so, its not checking against the invalid word list....

 

why are there 3 equal signs?

 

Link to comment
Share on other sites

The line:

if (strstr($strng, " ") === false)
      return " $col LIKE '%" . check_input($string) . "%' ";

 

checks to see if there are any spaces, and if there aren't, it returns the SQL query info.

 

The three equal signs in PHP mean "make sure these things are not only equal, but also the same type".

 

The need for === arises when a function can return both 0 and false.

 

A good example is strpos().  This function returns the location of a string within a string.  Ex:

 

$pos = strpos('hello', 'h');

 

This will return 0, since the string 'h' occcurs at position zero in the string 'hello'.

 

However

 

$pos = strpos('hello', 'x');

 

In this case, $pos will evaulate to false, because 'x' is not found in the string 'hello'.

 

So if you wanted to check to see if the string 'hello' contains the character 'h', this code would NOT work:

 

if (strpos('hello', 'h')) {
    // string found!
} 

 

Because even though the string 'h' is found in 'hello', strpos returns 0 which evaluates to false.  Therefore you need the === operator. (or in this case the !== operator)

 

if (strpos('hello', 'h') !== false) {
    // string found!
} 

 

Make sense?

Link to comment
Share on other sites

Err, sorry yeah, replace the $bad list with:

$bad = array('', 'or', 'the', 'and', 'a');

 

As far as that other code, the strstr() should check against the full array. There is always going to be more than one way to do something. Some ways are more efficient, others are easier to code.

 

Flyhoney gave a great explanation of the other code.

Link to comment
Share on other sites

@flyhoney: thank you for explaining that.....that does make sense....

 

so, in the code:

if (strstr($strng, " ") === false)

      return " $col LIKE '%" . check_input($string) . "%' ";

 

if it doesn't contain spaces (1 word or no words at all?)

then he returns the query.....

 

and the problem I'd have with that would be, if it is just one word, then, he puts it in the query without checking to see if it contains invalid words....

 

so, I should probably take that part out?

because won't the bottom part:

$words = explode(" ", strtolower($string));

  $statement = array();

  $notAllowed = array("and", "the", "or"); // add more if you like

  foreach ($words as $word) {

      if (!in_array($word, $notAllowed)) {

 

handle 1 word only?

Link to comment
Share on other sites

That bottom code will handle all of the words, but you are correct that if it's just one word, he sends it directly into the query.

ok, then I think I know how I need to modify it.....thanks guys.....

 

 

 

..nevermind the last part..i just tested it....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.