0

我需要一些有关 strpos() 的帮助。

需要建立一种方法来匹配任何包含/apple-touch但还需要保持细节匹配的 URL,例如"/favicon.gif"

目前,匹配项作为数组的一部分单独列出:

<?php 

$errorurl = $_SERVER['REQUEST_URI'];
$blacklist = array("/favicon.gif", "/favicon.png", "/apple-touch-icon-precomposed.png", "/apple-touch-icon.png", "/apple-touch-icon-72x72-precomposed.png", "/apple-touch-icon-72x72.png", "/apple-touch-icon-114x114-precomposed.png", "/apple-touch-icon-114x114.png", "/apple-touch-icon-57x57-precomposed.png", "/apple-touch-icon-57x57.png", "/crossdomain.xml");

 if (in_array($errorurl, $blacklist)) { // do nothing }
    else { // send an email about error }

?>

有任何想法吗?

非常感谢您的帮助

4

2 回答 2

1

除了正则表达式,您还可以删除所有出现的黑名单项目,str_replace并将新字符串与旧字符串进行比较:

if ( str_replace($blacklist, '', $errorurl) !== $errorurl )
{
  // do nothing
}
else
{
  // send an email about error
}
于 2012-10-11T02:30:16.767 回答
0

If you want to use regex for this, and you want a single regex string that will capture all the values in your existing blacklist plus match any apple-touch string, then something like this would do it.

if(preg_match('/^\/(favicon|crossdomain|apple-touch.*)\.(gif|png|xml)$/',$_SERVER['REQUEST_URI']) {
    //matched the blacklist!
}

To be honest, though, that's far more complex than you need.

I'd say you'd be better off keeping the specific values like favicon.gif etc in the blacklist array you already have; it'd make it a lot easier when you come to adding more items to the list.

I'd only consider using regex for the apple-touch values, since you want to block any variant of them. But even with that, it would likely be simpler if you used strpos().

于 2012-10-17T13:33:40.773 回答