Using basename
suggest changebasename(): Given a string containing the path to a file or directory, this function will return the trailing name component.
This function returns only the last part of a URL:
$url = "http://example.com/project/controller/action/param1/param2";
$parts = basename($url);
// Output: param2
If the URL ends in a filename and you need the directory that contains it, combine basename() with dirname():
$url = "http://example.com/project/controller/action/param1/param2/index.php";
$parts = basename(dirname($url));
// Output: param2
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents