I'm trying to detect the current section of a site that a user is viewing by checking for the final directory in the URL. I'm using a PHP and regex to do it and I think I'm close but unfortunately not quite there yet.
Here's what I currently have:
<?php
$url = $_SERVER['REQUEST_URI_PATH'] = preg_replace('/\\?.*/', '', $_SERVER['REQUEST_URI']);
$one = '/one/';
$two = '/three/';
$three = '/three/';
$four = '/four/';
$five = '/five/';
echo $url;
if (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($one)) == $one) {
// URI path starts with "/one/"
echo "The section is one.";
}
elseif (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($two)) == $two) {
// URI path starts with "/two/"
echo "The section is two.";
}
elseif (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($three)) == $three) {
// URI path starts with "/three/"
echo "The section is three.";
}
elseif (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($four)) == $four) {
// URI path starts with "/four/"
echo "The section is four.";
}
elseif (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($five)) == $five) {
// URI path starts with "/five/"
echo "The section is five.";
}
?>
I've placed in the echo before the if statements just to get confirmation of the value of $url. This outputs
/currentdirectory/file.php
However the conditions themselves don't match anything and my individual echo for each section never displays.
Also if there's a simpler way of doing it then I'm open to suggestions.
Thanks