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 can PHP developers ensure proper data handling and security when processing user inputs from forms?
- How can the error message "Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE" be resolved in PHP?
- What are the differences between using $_GET and $_POST in PHP form actions, and when should each be used?