如何在php if语句中使用多个路径

时间:2013-07-24 01:20:36

标签: php

<?php if (preg_match('/\/contact\//', $_SERVER['REQUEST_URI']) === 1): ?>
<a href="/">link</a>
<?php endif; ?>

我正在尝试将多个文件夹添加到同一语句中,例如/ contact /和/ news /,因此语句中的内容将出现在两个文件夹中。

我尝试了(preg_match('/\/contact\//', '/\/news\//', $_SERVER['REQUEST_URI']),但它返回了错误。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以在regexp中使用A |(OR)运算符。

<?php if (preg_match('/\/(contact|news)\//', $_SERVER['REQUEST_URI']) === 1): ?>

答案 1 :(得分:1)

这不是preg_match的工作方式。第一个参数是REGEX,第二个参数是主题。使用正则表达式|运算符

<?php if (preg_match('/\/(contact|news)\//', $_SERVER['REQUEST_URI']) === 1): ?>
   <a href="/">link</a>
<?php endif; ?>