PHP Using explode()


Example

explode(): Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

This function is pretty much straight forward.

$url = "http://example.com/project/controller/action/param1/param2";
$parts = explode('/', $url);

Array
(
    [0] => http:
    [1] => 
    [2] => example.com
    [3] => project
    [4] => controller
    [5] => action
    [6] => param1
    [7] => param2
)

You can retrieve the last part of the URL by doing this:

$last = end($parts);
// Output: param2

You can also navigate inside the array by using sizeof() in combination with a math operator like this:

echo $parts[sizeof($parts)-2];
// Output: param1