As you code PHP you will most likely get your self in a position where you need to break down an URL into several pieces. There's obviously more than one way of doing it depending on your needs. This article will explain those ways for you so you can find what works best for you.
basename(): Given a string containing the path to a file or directory,
this function will return the trailing name component.
This function will return only the last part of an URL
If your URL has more stuff to it and what you need is the dir name containing the file you can use it with dirname() like this:
explode(): Returns an array of strings, each of which is a substring of
string formed by splitting it on boundaries formed by the string
This function is pretty much straight forward.
You can retrieve the last part of the URL by doing this:
You can also navigate inside the array by using sizeof() in combination with a math operator like this:
parse_url(): This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
If you need the path separated you can use explode
If you need the last part of the section you can use end() like this:
If the URL contains GET vars you can retrieve those as well
If you wish to break down the query vars you can use parse_str() like this:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0