在 PHP 大海捞针中用多针编写 strpos 的更短方法
Shorter way to write strpos with multiple needles in the haystack in PHP
我正在查看大海捞针 /cgi-bin /css etc
中是否有针 $dir
。
为了检查多根针,我必须在每个strpos()
之后写||
。
我在这里看到了一些其他线程,但是 none 确实显示了最简单的代码。
我的印象是,就简单性和最小化代码而言,这是比较少量 (10 或更少) 多针的最佳方法用 strpos 去大海捞针。
如果您知道更短且更受欢迎的方法,请在下面留下答案。
但没有额外复杂的代码。一个数组就可以了,但要保持简单和简短。
代码:
if (strpos($dir, '/cgi-bin') || strpos($dir, '/css') || strpos($dir, '/images') || strpos($dir, '/includes') || strpos($dir, '/test') !== false)
continue;
使用正则表达式:
if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) {
continue;
}
我正在查看大海捞针 /cgi-bin /css etc
中是否有针 $dir
。
为了检查多根针,我必须在每个strpos()
之后写||
。
我在这里看到了一些其他线程,但是 none 确实显示了最简单的代码。
我的印象是,就简单性和最小化代码而言,这是比较少量 (10 或更少) 多针的最佳方法用 strpos 去大海捞针。
如果您知道更短且更受欢迎的方法,请在下面留下答案。
但没有额外复杂的代码。一个数组就可以了,但要保持简单和简短。
代码:
if (strpos($dir, '/cgi-bin') || strpos($dir, '/css') || strpos($dir, '/images') || strpos($dir, '/includes') || strpos($dir, '/test') !== false)
continue;
使用正则表达式:
if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) {
continue;
}