
|
View Full Version : [php] Checking 'if equals' against a list...
jonathanbull 07-02-2005, 12:34 PM Hi,
I wish to have a list of words, say for example a list of fruits. A user will enter a word and if their text matches any of the words on this list then "fruit exists" will be echoed. So basically, I'm just checking a variable against a number of variables in a list.
I'm finding this kinda hard to explain so I hope the example below makes sense:
$list = apple, kiwi, melon;
if ($a == $list) {
echo "fruit exists";
}
I'm sure there must be a graceful way of doing this in php, but I can't find any clues on the net.
Can anybody help me out? Any help is greatly appreciated!
Thanks a lot,
Jon.
azizny 07-02-2005, 12:42 PM there are many ways to do it:
$array = "apple,kiwi,melon";
if(eregi($fruit,$array) echo "exists";
another way:
$array = array("apple","kiwi","melon");
foreach($array in $fruits)
if($fruit == $fruits) echo "exist"
or maybe
$array["kiwi"];$array["melon"];
if($array[$fruit]) echo "exist"
Peace,
laserlight 07-02-2005, 01:17 PM Suppose you have an array:
$list = array('apple', 'kiwi', 'melon');
Suppose also that you want to find a case-sensitive match.
Then you can use:
if (in_array($a, $list)) {
echo 'There is a match';
}
chrisblack 07-02-2005, 04:06 PM Take a look at the strstr() function.
$list = 'apple, kiwi, melon';
if (strstr($list,$a)) {
echo "fruit exists";
}
Chris
laserlight 07-03-2005, 12:10 AM Actually, if you intend it to be a string rather than an array, then strpos() would be better than strstr().
if (strpos($a, $list) !== false) {
echo 'There is a match';
}
You could also use stripos() for case-insensitive matching.
mfonda 07-03-2005, 02:21 AM /me points to http://us4.php.net/manual/en/function.in-array.php ...
Dan L 07-03-2005, 11:16 AM Matt, he's looking for data inside a list, not an array, so that probably wouldn't help. Unless he's actually using an array, and isn't telling anyone. :)
ForgottenCreature 07-03-2005, 12:01 PM You could always seperate the data using the explode() function.
us2.php.net/manual/en/function.explode.php
array explode ( string separator, string string [, int limit] )
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
You would then have an array, and you could use any method
azizny stated.
here are many ways to do it:
$array = "apple,kiwi,melon";
if(eregi($fruit,$array) echo "exists";
another way:
$array = array("apple","kiwi","melon");
foreach($array in $fruits)
if($fruit == $fruits) echo "exist"
or maybe
$array["kiwi"];$array["melon"];
if($array[$fruit]) echo "exist"
jonathanbull 07-04-2005, 11:55 PM Thanks very much everybody.
I didn't anticipate receiving so many replies! I went with azizny's method as it was the first and works perfectly.
Thanks again,
Jon.
laserlight 07-05-2005, 07:02 AM azizny's eregi method should work fine, as you have noticed. However, in this case regular expressions is overkill, so using strpos() is better.
in_array() is PHP's native implementation of what azizny described in his/her 2nd example.
|