questy
questy

Reputation: 527

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.

I tried (preg_match('/\/contact\//', '/\/news\//', $_SERVER['REQUEST_URI']), but it returned errors.

What am I doing wrong?

Upvotes: 0

Views: 60

Answers (2)

Martin.
Martin.

Reputation: 10539

That's not how preg_match works. First argument is REGEX and the second one is a subject. Use regex | operator

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

Upvotes: 1

DevZer0
DevZer0

Reputation: 13535

You can use A | (OR) operator in regexp.

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

Upvotes: 1

Related Questions