Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list and explode

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!

like image 231
cskwrd Avatar asked Mar 01 '10 18:03

cskwrd


People also ask

What is explode () in Python?

The explode() method converts each element of the specified column(s) into a row.

How do you explode a list in Python?

The explode() is a Python function used to transform or modify each member of an array or list into a row. The explode() function converts the list elements to a row while replacing the index values and returning the DataFrame exploded lists.

Why explode () is used?

The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

What does the explode () function return?

The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.


1 Answers

Try this :

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

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

like image 163
Snoozer Avatar answered Oct 05 '22 13:10

Snoozer