我正在开发一个小函数来接收一个 url 并根据它所在的位置返回一个相对路径。
如果 url 在查询字符串中包含路径,则 pathinfo
返回错误的结果。下面的代码演示了这一点:
$p = 'http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir/afile.txt';
$my_path_info = pathinfo($p);
echo $p . '<br/><pre>';
print_r($my_path_info);
echo '</pre>';
该代码输出:
http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir/afile.txt
Array
(
[dirname] => http://localhost/demos/image_editor/dir_adjuster.php?u=http://localhost/demos/some/dir
[basename] => afile.txt
[extension] => txt
[filename] => afile
)
这显然是错误的。任何解决方法?
最佳答案
Any workaround?
是的,这样做 right ;)
$url = urlencode('http://localhost/demos/some/dir/afile.txt');
$p = 'http://localhost/demos/image_editor/dir_adjuster.php?u='.$url;
对于 URL,尤其是带有查询字符串的 URL,
parse_url()
应该更可靠地提取路径分量;之后,运行 pathinfo()
在上面。
关于PHP pathinfo 被查询字符串中的 url 所迷惑,有什么解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3013454/