Reputation: 35
Right now I use stristr($q, $string)
but if
$string = "one monkey can jump 66 times";
$q = "monkey 66";
I want to find out if this string contains both monkey
and 66
.
How can i do that?
Upvotes: 0
Views: 5400
Reputation: 3621
You can use the strpos()
function which is used to find the occurrence of one string inside another one:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
Note that the use of !== false
is deliberate (neither != false
nor === true
will work); strpos()
returns either the offset at which the needle string begins in the haystack string, or the boolean false
if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are')
.
Upvotes: 0
Reputation: 467
you could use both stristr and strpos.
as it is reported in this post, the second method is faster and less memory intensive.
well, check this lines out:
// here there are your string and your keywords
$string = "one monkey can jump 66 times";
$q = "monkey 66";
// initializate an array from keywords in $q
$q = explode(" ", $q);
// for every keyword you entered
foreach($q as $value) {
// if strpos finds the value on the string and return true
if (strpos($string, $value))
// add the found value to a new array
$found[] = $value;
}
// if all the values are found and therefore added to the array,
// the new array should match the same object of the values array
if ($found === $q) {
// let's go through your path, super-man!
echo "ok, all q values are in string var, you can continue...";
}
Upvotes: 2
Reputation: 838
simply post your variable value by giving them a variable $monkey,$value ($monkey jumps $value) and then fetch its value
Upvotes: -1
Reputation: 573
if(stristr('monkey', $string) && stristr('66', $string)) {
//Do stuff
}
Upvotes: 1