list and explode

arrays, explode, list, php, url-rewriting

Solution

You should check how many path segments the URL contains:

$segments = explode('/', $url);
if (count($segments) !== 2) {
    // error
} else {
    list($dir, $act) = $segments;
}

But maybe you should choose a more flexible approach than using `list`.

Problem

I am trying to use url rewriting on my website and I want to use the `list()` and `explode()` functions to get the right content. Currently my code looks like this: ``` list($dir, $act) = explode('/',$url); ``` In this case `$url` is equal to everything after the first slash in the absolute url i.e. `http://example.com/random/stuff => $url = random/stuff/` this would work fine, but if I want to go to `http://example.com/random/` then it will print a notice on the page. How do I stop the notice from showing up do I need to use something other than the `list()` function? Right now the notice is "Notice: Undefined offset: 1..." Thanks for the help!

Original source