views:

104

answers:

6

Hey everyone,

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!

+1  A: 

A solution would be to split your line of code in several, to ensure you never assign non-existing values to variables -- which is what you are doing when explode only returns one portion of URL.

For that, not using list seems like the right solution, as, with list, you must know how many elements the expression on the right of = will return...
And, in this situation, you don't know how many elements explode will return.


For instance, something like this might be OK :

$parts = explode('/', $url);
if (isset($parts[0])) {
  $dir = $parts[0];
  if (isset($parts[1])) {
    $act = $parts[1];
  }
}

Of course, up to you to deal with the situation in which $dir and/or $act are not set, later in your script.


Another solution would be to check how many elements explode will return (counting a number of / for instance) ; but you'll still have to deal with at least two cases.

Pascal MARTIN
+4  A: 

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.

Gumbo
What would be an example of a more flexible approach?
TheGNUGuy
@TheGNUGuy: That depends on your requirements, how your application expect to receive its arguments.
Gumbo
@Gumbo I adapted your solution above to use a switch statement so more easily deal with multiple different cases. Thanks!
TheGNUGuy
A: 

Instead of exploding the full URL, try $_SERVER['PATH_INFO'], assuming '/random' names the script.

outis
Downvoter: what's wrong with PATH_INFO? The question is ambiguous enough to allow for this possibility.
outis
A: 

The simple and ugly answer is to prefix the whole thing with a single '@' which suppresses error outputs. $act will be set to null in that case because under the hood, it's equivalent to:

$foo = explode('/',$url);
$dir = $foo[0];
$act = $foo[1]; // This is where the notice comes from
Luke
A: 

to get rid of the notice:

list($dir, $act) = explode('/',$url);

but maybe a better solution would be:

$segments = explode ('/', $url);
$dir = array_shift ($segments);
$act = array_shift ($segments);

if there is no 2nd segment, $act would be null and you can also more than 2 segment this way

mathroc
A: 

Check out parse_url

dnagirl