list and explode

0 votes
asked Mar 1, 2010 by cskwrd

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!

8 Answers

0 votes
answered Jan 1, 2010 by pascal-martin

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.

0 votes
answered Jan 1, 2010 by outis

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

0 votes
answered Jan 1, 2010 by luke

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
0 votes
answered Jan 1, 2010 by mathieu

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

0 votes
answered Jan 1, 2010 by dnagirl

Check out parse_url

0 votes
answered Mar 1, 2010 by gumbo

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.

0 votes
answered Mar 10, 2014 by snoozer

Try this :

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

array_pad complete the array with your values, here is : ''.

0 votes
answered Jan 6, 2016 by giosh94mhz

The correct way to suppress the E_NOTICE on list assignment as of PHP 5.4+ is the following:

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

If $url contains no /, explode will return one element and $act will be NULL.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...