简体   繁体   English

PHP-有比使用多个IF更好的搜索大字符串的方法

[英]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. 我有一个名为$data的变量,该变量存储大量数据。 I know that data will contain ONE of these words… cheese , crackers , or fruit . 我知道数据将包含以下词语之一: cheesecrackersfruit 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?). 因此,如果未找到食物,则最终要对食物变量进行最后3次的搜索(我是正确的,它是如何工作的?)。

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. 我希望能够将food变量设置为找到的变量。 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? 还是PHP仍在搜索3次以查找每个项目? 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. 我先使用IF语句,然后使用Jammy和Jason发布的array方法,再使用Daren发布的preg_match方法,对原始方法进行了一些时序测试。 I was quite surprised. 我很惊讶。 PHP is SUPER FAST no matter what method you use. 无论使用哪种方法,PHP都是超级快速的。 I created a variable called $data that was 1meg long and seached for any of 10 words. 我创建了一个名为$ data的变量,该变量长1兆,并针对10个单词中的任何一个进行了填充。 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. 因此,即使在遍历所有数据10次之后,IF和数组循环也至少比preg match快10倍,但是Pregmatch仍然会完成,然后您才能眨眼。

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. 然后您可以根据结果影响食物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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