list and explode

cskwrd picture cskwrd · Mar 1, 2010 · Viewed 34.2k times · Source

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!

Answer

Snoozer picture Snoozer · Nov 10, 2014

Try this :

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

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