Page 1 of 1
if $var is in $pancake
Posted: Thu Jan 22, 2004 8:10 am
by lizlazloz
ok.
what code can i use to find this, if it is possible...
Code: Select all
<?php
$var="cheese";
$pancakerecipe="eggs,rice,cheese,corn";
if ( $var is in $pancakerecipe)
{
$boolean="true";
}
else
{
$boolean="false";
}
?>
i'm guessing that 'is in' isnt the proper code... but what is?
Posted: Thu Jan 22, 2004 8:17 am
by malcolmboston
sorry i have no idea what $boolean is for
the
explode() function might be of use here
then try this
Code: Select all
<?php
$var ="cheese";
$pancakerecipe = "Pancake Recipe
$pancakerecipe="eggs,rice,cheese,corn";
if ( $var is in $pancakerecipe)
{
print "$var is in the recipe for $pancakerecipe";
}
else
{
print ="$var is not in the recipe for $pancakerecipe";
}
?>
i havent done anything like this but the explode function i believe can do something to this effect
Posted: Thu Jan 22, 2004 8:28 am
by redmonkey
stristr() is the first way that springs to mind. This function does not match case, if you need a case-sensitive version you can use strstr()
Code: Select all
<?php
$var="cheese";
$pancakerecipe="eggs,rice,cheese,corn";
if (stristr($pancakerecipe, $var))
{
$boolean="true";
}
else
{
$boolean="false";
}
?>
Posted: Thu Jan 22, 2004 8:41 am
by junky
personnaly, i would put in in a function, but if ya dont want a function, ya could use that code too:
Code: Select all
<?php
$var = "Cheese";
$var = strtolower($var); //to be sure to catch that var even if its in caps
$pancakerecipe= array("eggs","rice","cheese","corn");
if ( in_array($var, $pancakerecipe) ) {
$boolean="true";
print "{$var} was found in the pancakerecipe";
}
else {
$boolean="false";
print "{$var} was NOT found in the pancakerecipe";
}
?>
the is_array function is more detailled at:
http://www.php.net/manual/en/function.in-array.php
later
Re: if $var is in $pancake
Posted: Thu Jan 22, 2004 10:15 am
by McGruff
Code: Select all
<?php
function isRecipeItem($item, $recipe)
{
if (substr_count($item, $recipe) > 0)
{
return true; // don't quote if you want a boolean value rather than a string
}
else
{
return false;
}
}
// in use
if(isRecipeItem($item, $recipe) == true)
{
// do something
}
?>
Posted: Thu Jan 22, 2004 2:59 pm
by dull1554
i would suggest using an array if its a possibility
Posted: Thu Jan 22, 2004 3:03 pm
by markl999
Code: Select all
$var='cheese';
$pancakerecipe='eggs,rice,cheese,corn';
if(in_array($var, explode(',', $pancakerecipe))){
...TRUE...
} else {
...FALSE...
}