Forum Moderators: coopster

Message Too Old, No Replies

why do I have an extra item in my array?

trying to explode url to get sections

         

HelenDev

1:47 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why is my code...

$urlarray = explode("/", "/about/new/index.php");
print_r($urlarray);

...returning this...


Array ( [0] => [1] => about [2] => new [3] => index.php )

Perhaps I am being dense but why do I have an extra empty value at the beginning of the array? I was expecting only 3 items.

Is there any way I can make it return the ones I want (ie only those containing an actual value) or do I simply have to work around this?

Habtom

1:50 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$urlarray = explode("/", "/about/new/index.php");
print_r($urlarray);

Removing the first backslash will give you the result you are looking for. $urlarray[0] is the empty value before the first backslash, so removing it will remove the $urlarray[0].

It should be like this:
$urlarray = explode("/", "about/new/index.php");
print_r($urlarray);

Habtom

HelenDev

2:05 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Habtom. I wondered if it was something like that.

The problem is that I'm actually using $_SERVER["PHP_SELF"] to get the URL. What's the easiest way to remove the initial slash from this?

jatar_k

2:20 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[php.net...]

$url = substr($_SERVER['PHP_SELF'],1);

that will return the string minus the first char

HelenDev

2:27 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k. Perfect :)