views:

170

answers:

3

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

+1  A: 

Why not use dirname?

Emil Vikström
Because until now I didn't know it existed. :) I'm just learning as I go but that's a useful function and a much better solution (see below). Thanks for pointing that out.
Ian
A: 

Ok, I've got my original solution working now:

<?php
    $_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.";
    }
?>

But following on from Emil's comment I now see that there's a much simpler method and I also got this working:

<?php   
    $section = dirname($_SERVER['PHP_SELF']) . "/";

    if ($section == "/one/") {
        echo "one";
    } elseif ($section == "/two/") {
        echo "two";
    } elseif ($section == "/three/") {
        echo "three";
    } elseif ($section == "/four/") {
        echo "four";
    } elseif ($section == "/five/") {
        echo "five";
    } elseif ($section == "//") {
        echo "global";
    }
?>

I just thought I'd post both methods up here in case they are of use to someone else.

Ian
Another useful language construct is switch, which can replace your elseifs: http://php.net/switch
Emil Vikström
A: 

Or you can do it with parse_url

Benoit
Another good option which could provide more information in the form of an associative array. Probably more than I need in this particular case but it would still do the job none-the-less.Thanks
Ian