简体   繁体   English

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

[英]how can I use multiple paths in php if statement

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

I'm trying to add multiple folders into the same statement like /contact/ and /news/, so the content inside the statement will appear in both folders. 我正在尝试将多个文件夹添加到同一语句中,例如/ contact /和/ news /,因此语句中的内容将出现在两个文件夹中。

I tried (preg_match('/\\/contact\\//', '/\\/news\\//', $_SERVER['REQUEST_URI']) , but it returned errors. 我试过(preg_match('/\\/contact\\//', '/\\/news\\//', $_SERVER['REQUEST_URI']) ,但它返回了错误。

What am I doing wrong? 我究竟做错了什么?

You can use A | 你可以使用A | (OR) operator in regexp. 正则表达式中的(OR)运算符。

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

That's not how preg_match works. 这不是preg_match工作方式。 First argument is REGEX and the second one is a subject. 第一个参数是REGEX,第二个参数是主题。 Use regex | 使用正则表达式| operator 操作者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM