Home > Backend Development > PHP Tutorial > How to Extract the Last Part of a URL in PHP?

How to Extract the Last Part of a URL in PHP?

Patricia Arquette
Release: 2024-11-09 03:54:02
Original
515 people have browsed it

How to Extract the Last Part of a URL in PHP?

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');
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template