CheeseFlavored
CheeseFlavored

Reputation: 2112

PHP - Is there a better way to search a large string than with multiple IFs

I have a variable called $data that is storing a large amount of data. I know that data will contain ONE of these words… cheese, crackers, or fruit. Or, it could possibly contain none of those words. Right now, I do this….

if (strpos($data,"cheese")!==false) $food="cheese";
else if (strpos($data,"crackers")!==false) $food="crackers";
else if (strpos($data,"fruit")!==false) $food="fruit";
else $food=”none”;

So, if cheese is found, the data is only being searched once. If cheese isn’t found, the data is being searched again, because it first looked for cheese, didn't find it, then had to search again to look for crackers. See the problem? So, if no food is found, the data ends up being searched 3 times before the food variable is finally set (am I right, is that how it works?).

I’m wondering if there’s a more efficient way to search. I thought of a possible way, but I don’t know how to do it… What if I searched for all three foods at once like this…

if (strpos($data,"cheese")!==false or
strpos($data,"crackers")!==false or
strpos($data,"fruit")!==false)
$food=THE WORD THAT WAS FOUND GOES HERE

I want to be able to set the food variable to what was found. If this is possible, Am I correct in thinking this would be much faster because you’re searching the data only once? Or is PHP still searching 3 times to look for each item? And is it even possible to set the food variable to what was found?

Upvotes: 0

Views: 132

Answers (5)

CheeseFlavored
CheeseFlavored

Reputation: 2112

This turned out to be an interesting experiment. I ran some timing tests on my original method with the IF statements, then with the array method posted by Jammy and Jason, then with the preg_match method posted by Daren. I was quite surprised. PHP is SUPER FAST no matter what method you use. I created a variable called $data that was 1meg long and seached for any of 10 words. when no words were found I got this for average time...

If_method = 0.01 seconds
Array_Method = 0.006 seconds
Preg_match = 0.1 seconds

So, even after looping through all that data 10 times, the IFs and array loops were at least 10 times faster than preg match, but Pregmatch is still going to finish before you can blink.

Upvotes: 0

Daren Chandisingh
Daren Chandisingh

Reputation: 2165

This is simpler. Not necessarily faster.

$food = 'none';
if (preg_match('#(cheese|crackers|fruit)#', $data, $match)) {
    $food = $match[1];
}

Upvotes: 1

Youssef Saoubou
Youssef Saoubou

Reputation: 591

This will give you all the positions of the words

$data = explode(' ', $data); 
$words= array('cheese', 'fruit', 'crackers');
var_dump(array_intersect($data, $words)); 

Then you can affect food based on the result.

Upvotes: -1

James McClelland
James McClelland

Reputation: 555

The best way would probably be to use an array:

$data = "cheese platter";
$toCheck = [
    "fruit",
    "crackers",
    "cheese"
];
$food = false;
foreach ($toCheck as $item){
    if (strpos($data, $item) !== false) {
        $food = $value;
        break;
    }
}`

Upvotes: 2

Jason Jiménez
Jason Jiménez

Reputation: 369

Why are you not using an array for that? It would be easier

$words = ["cheese","crackers","fruit"];
$food = null;
foreach ($words as $value){
    if (!$food && strpos($data, $value))
        $food = $value;
}

Upvotes: 3

Related Questions