我们可以很容易地沉默的错误(见@ John_Conde的答案),但这里的更大的问题是,你的代码是没有意义的。
您出现要尝试在您的REQUEST_URI中的任意位置查找两个字符串中的一个。但这不是URI的结构。
下面是您可能意思的严格翻译。然后我会解释为什么它是错误的。
function stripos_array($haystack, $needles, $offset=0) {
foreach ($needles as $needle) {
if (FALSE!==stripos($haystack,$needle,$offset)) {
return True;
}
}
return False;
}
$pages = array("random-number-generator", "calculator");
if (stripos_array($_SERVER['REQUEST_URI'], $pages) {
echo 'active';
}
这怎么可能,不可能有一个正确实施的你在做什么?看看一些样品:
stripos_array('/not-a-random-number-generator', $pages) // true!
stripos_array('/some/other/part/of/the/site?searchquery=calculator+page', $pages); // true!
stripos_array('/random-number-generator/calculator', $pages); // true!! but meaningless!!
我强烈怀疑你真正想要做的是使用一些真正 URL路由。这里有两种可能性:
使用查询参数;网址看起来像http://example.org/index.php?page=calculator
if (isset($_GET['page']) && in_array($_GET['page'], $pages)) ....
使用路径段;网址看起来像http://example.org/index.php/calculator
$path = trim($_SERVER['PATH_INFO'], '/');
$pathsegments = explode('/', $path);
if (isset($pathsegments[0]) && in_array($pathsegments, $pages)) ...
没错,但这个代码也是荒谬的 – 2012-07-05 18:22:08
谢谢你,现在的作品:) – Matty 2012-07-05 18:22:42
@Matty,没有也。 – 2012-07-05 18:23:07