简体   繁体   中英

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?

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;
}

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;
    }
}`

This is simpler. Not necessarily faster.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM