0

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?

Fallen
  • 4,435
  • 2
  • 26
  • 46

4 Answers4

2

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...";

}
Community
  • 1
  • 1
blurstream
  • 429
  • 3
  • 13
  • if ($found === $q) { // let's go through your path, super-man! echo "ok, all q values are in string var, you can continue..."; } it's an array, you cant compare it to $q or? – Anders Nilsson Aug 13 '14 at 06:54
  • $q is became an array when we exploded it: $q = explode(" ", $q); so the === checks if two objects are identical – blurstream Aug 13 '14 at 07:03
1
if(stristr('monkey', $string) && stristr('66', $string)) {
    //Do stuff
}
Dennis Schepers
  • 573
  • 3
  • 18
0

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').

Yohanim
  • 3,319
  • 9
  • 52
  • 92
-1

simply post your variable value by giving them a variable $monkey,$value ($monkey jumps $value) and then fetch its value

Aman
  • 806
  • 2
  • 12
  • 38
  • This is a very.. Very long process, functionality of the site would seriously drop due to the amount of inputs – Daryl Gill Aug 12 '14 at 13:52
  • no it can simply achieve it by ajax just post the value by given id and + (add)it by another value then simply post it via datastream – Aman Aug 12 '14 at 13:56