How can the basename function be used to extract the file name from a URL in PHP?
To extract the file name from a URL in PHP, you can use the basename function. This function takes a string (in this case, the URL) and returns the last component of the path, which in this case would be the file name. By using basename, you can easily extract the file name from a URL without having to manually parse the string.
$url = "https://www.example.com/path/to/file.txt";
$fileName = basename($url);
echo $fileName; // Output: file.txt
Keywords
Related Questions
- How does the use of static variables and functions impact performance in PHP, and what are the best practices for optimizing code?
- Are there any best practices for validating Public Keys before importing them in PHP?
- What potential issues can arise when displaying images using PHP, especially in Safari or Chrome browsers?