你如何执行preg_match,该模式是一个数组,在PHP? [英] How do you perform a preg_match where the pattern is an array, in php?

查看:119
本文介绍了你如何执行preg_match,该模式是一个数组,在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组满,我需要匹配的模式。任何方式做到这一点,比for()循环其他?我试着做了至少CPU密集型方式,因为我会做这些几十每分钟。

I have an array full of patterns that I need matched. Any way to do that, other than a for() loop? Im trying to do it in the least CPU intensive way, since I will be doing dozens of these every minute.

现实世界的例子是,林建一个链接状态检查,这将检查链接到各种在线视频网站,以确保影片仍然生活。每个域都有几个死关键字,如果这些在一个页面的HTML,这意味着该文件已被删除找到。这些存储在数组中为止。我需要的内容相匹配的阵列PF,对页面的HTML输出。

Real world example is, Im building a link status checker, which will check links to various online video sites, to ensure that the videos are still live. Each domain has several "dead keywords", if these are found in the html of a page, that means the file was deleted. These are stored in the array. I need to match the contents pf the array, against the html output of the page.

推荐答案

首先,如果你从字面上只是做几十每个分钟,然后我就不会担心可怕关于在这种情况下的性能。这些比赛是pretty快,我不认为你会通过你的模式遍历数组并分别调用preg_match这样有性能问题:

First of all, if you literally are only doing dozens every minute, then I wouldn't worry terribly about the performance in this case. These matches are pretty quick, and I don't think you're going to have a performance problem by iterating through your patterns array and calling preg_match separately like this:

$matches = false;
foreach ($pattern_array as $pattern)
{
  if (preg_match($pattern, $page))
  {
    $matches = true;
  } 
}

您可以用确实像有些人的运营商所提出的建议所有的图案组合成一个,但不只是一个 | 。这将破坏严重,如果你的任何图案的包含 or运算符。

You can indeed combine all the patterns into one using the or operator like some people are suggesting, but don't just slap them together with a |. This will break badly if any of your patterns contain the or operator.

我建议至少使用分组括号喜欢你的方式:

I would recommend at least grouping your patterns using parenthesis like:

foreach ($patterns as $pattern)
{
  $grouped_patterns[] = "(" . $pattern . ")";
}
$master_pattern = implode($grouped_patterns, "|");

但是...我真的不知道,如果这最终被速度更快。 的东西必须通过他们的循环,不管它是preg_match或PHP。如果我猜我猜个人比赛将接近快,更容易阅读和维护。

But... I'm not really sure if this ends up being faster. Something has to loop through them, whether it's the preg_match or PHP. If I had to guess I'd guess that individual matches would be close to as fast and easier to read and maintain.

最后,如果性能是你在找什么在这里,我觉得做的最重要的事情是拔出非正则表达式匹配到一个简单的字符串包含检查。我可以想象你的一些支票必须是简单的字符串检查,像看看是否这个网站被关闭的网页上。

Lastly, if performance is what you're looking for here, I think the most important thing to do is pull out the non regex matches into a simple "string contains" check. I would imagine that some of your checks must be simple string checks like looking to see if "This Site is Closed" is on the page.

所以这样做:

foreach ($strings_to_match as $string_to_match)
{
  if (strpos($page, $string_to_match) !== false))
  {
    // etc.
    break;
  }
}
foreach ($pattern_array as $pattern)
{
  if (preg_match($pattern, $page))
  {
    // etc.
    break;
  } 
}

和避免尽可能多的 preg_match()尽可能或许将是你最好的收益。 strpos()许多 preg_match快()

and avoiding as many preg_match() as possible is probably going to be your best gain. strpos() is a lot faster than preg_match().

这篇关于你如何执行preg_match,该模式是一个数组,在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆