Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, if you literally are only doing <strong>dozens</strong> every <strong>minute</strong>, 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:</p> <pre><code>$matches = false; foreach ($pattern_array as $pattern) { if (preg_match($pattern, $page)) { $matches = true; } } </code></pre> <p>You can indeed combine all the patterns into one using the <code>or</code> operator like some people are suggesting, but don't just slap them together with a <code>|</code>. This will break badly if any of your patterns <strong>contain</strong> the or operator.</p> <p>I would recommend at least grouping your patterns using parenthesis like:</p> <pre><code>foreach ($patterns as $pattern) { $grouped_patterns[] = "(" . $pattern . ")"; } $master_pattern = implode($grouped_patterns, "|"); </code></pre> <p>But... I'm not really sure if this ends up being faster. <strong>Something</strong> 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.</p> <p>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.</p> <p>So doing this:</p> <pre><code>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; } } </code></pre> <p>and avoiding as many <code>preg_match()</code> as possible is probably going to be your best gain. <code>strpos()</code> is a <strong>lot</strong> faster than <code>preg_match()</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload