1

I'm checking to see if any of the needles /cgi-bin /css etc are in the haystack $dir.

In order to check on multiple needles, I have to write the || after each strpos().

I've seen some other threads on here, but none that really showed the most simplest code.

I'm under the impression that as far as simplicity and minimized code, this is the best way to go about comparing a low quantity (10 or less) of multiple needles to the haystack with strpos.

If you know of a shorter and more preferred way to do this please leave an answer below.

But no extra complicated code. An array would be fine, but keep it simple and short.

Code:

if (strpos($dir, '/cgi-bin') || strpos($dir, '/css') || strpos($dir, '/images') || strpos($dir, '/includes') || strpos($dir, '/test') !== false)
continue;
Mike
  • 607
  • 8
  • 30

1 Answers1

2

Use a regular expression:

if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) {
    continue;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That is absolutely it! But what does the `?:` mean before the `cgi-bin` needle? – Mike May 07 '15 at 01:25
  • Also can I just write it like this instead without the curly brackets? `if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) continue;` – Mike May 07 '15 at 01:29
  • `?:` after a left parenthesis makes the group non-capturing: http://stackoverflow.com/questions/3512471/non-capturing-group. You can write it without the curly braces, but I don't recommend it: http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar May 07 '15 at 16:17