Last Part of URL Extraction in PHP
Determining the last part of a URL is often useful in retrieving specific information. This article addresses how to extract the last part of a URL in PHP using the basename() function.
Problem:
Given a URL like "http://domain.example/artist/song/music-videos/song-title/9393903," how can you extract the final part, "9393903," using PHP?
Solution:
The basename() function can be used to extract the final part of a URL. It typically returns the last filename or directory from a given path. In this case, the URL can be considered a path.
To use basename(), simply pass the URL as an argument to the function like so:
echo basename('http://example.com/artist/song/music-videos/song-title/9393903');
This will print the last part of the URL, which is "9393903."
Note: If the URL contains a query string, the basename() function will also include it in the returned value. For example, if the URL is "http://example.com/artist/song/music-videos/song-title/9393903?query=string," the basename() function will return "9393903?query=string."
The above is the detailed content of How to Extract the Last Part of a URL in PHP?. For more information, please follow other related articles on the PHP Chinese website!