1

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?

1
  • do you want to search similar words in both string ?? Commented Aug 12, 2014 at 12:51

4 Answers 4

3

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
$q is became an array when we exploded it: $q = explode(" ", $q); so the === checks if two objects are identical
1
if(stristr('monkey', $string) && stristr('66', $string)) {
    //Do stuff
}

3 Comments

Well, that don't work. I use ajax to send the variable from a text input. So if I want to know if the string contains both "monkey" and "66" i must explode $q first to an array.
What doesn't work about it? Please be more explicit.
@AndersNilsson If you already know that you have to explode it, why do you ask?
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').

Comments

-1

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

2 Comments

This is a very.. Very long process, functionality of the site would seriously drop due to the amount of inputs
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.