How can the pathinfo() function in PHP be utilized to extract file path information in a more elegant manner compared to strpos() and substr()?
When extracting file path information in PHP, using the pathinfo() function is more elegant than using strpos() and substr() because pathinfo() returns an associative array containing information about the path, such as the directory name, filename, extension, and basename. This makes it easier to access specific parts of the file path without manually parsing the string.
$file_path = '/path/to/file.txt';
$path_info = pathinfo($file_path);
$directory_name = $path_info['dirname'];
$filename = $path_info['filename'];
$extension = $path_info['extension'];
$basename = $path_info['basename'];
echo "Directory: $directory_name\n";
echo "Filename: $filename\n";
echo "Extension: $extension\n";
echo "Basename: $basename\n";