Sam Sam - 2 months ago 14
PHP Question

Server Request URI WildCard Magento

I'm currently using the following code to generate an active state on links:

<?php if ($_SERVER["REQUEST_URI"] == "/styles/bracelets.html/"):?>

Followed by an else if statement.
How would I add a wild card so that the path above includes all pages with a path of: /styles/bracelets.html?p=2, /styles/bracelets.html?p=3 etc.

Thanks in advance


You could use strpos() in this case:

if (strpos($_SERVER['REQUEST_URI'], '/styles/bracelets.html/') === 0):

This basically checks to make sure /styles/bracelets.html/ is at the beginning of the request URI and will also pass if there is additional content trailing the / on the end. strpos will return false if the need is not found in haystack, or 0 if it is found at the beginning of the haystack hence the need for ===.

preg_match() would also work, but strpos does the trick in this case.