Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if URL ends in /site-map

Tags:

php

I need determine if the current URL ends in /site-map

For example: site.com/site-map

Or

site.com/somedirectory/site-map

Is there a PHP method to pull this value?

like image 879
Scott B Avatar asked Dec 22 '22 12:12

Scott B


2 Answers

You could use substr to check the last 9 characters:

$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map")

edit to accommodate the url ending with /site-map/ occasionally you could do this:

$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map" || substr($url,-10)=="/site-map/")
like image 192
Niklas Avatar answered Dec 24 '22 00:12

Niklas


Here's a preg_match() solution. Likely to be a bit slower than strpos() & substr(), but more flexible.

$url = $_SERVER['REQUEST_URI'];
if (preg_match("/\/site-map$/", $url)) {
  // it ends in /site-map
}
like image 40
Michael Berkowski Avatar answered Dec 24 '22 01:12

Michael Berkowski