PHP pathinfo 被查询字符串中的 url 欺骗了,有什么解决方法吗?
我正在开发一个小函数来接收 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
)
这显然是错误的。有什么解决办法吗?
I am working on a small function to take in a url and return a relative path based on where it resides itself.
If the url contains a path in the query string, pathinfo
returns incorrect results. This is demonstrated by the code below:
$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>';
That code outputs:
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
)
Which obviously is wrong. Any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这样做正确;)
对于 URL,尤其是带有查询字符串的 URL,
parse_url()
应该更可靠地提取路径组件;之后,对其运行pathinfo()
。Yeah, doing it right ;)
and for URLs, especially those with query strings,
parse_url()
should be more reliable to extract the path component; After that, run thepathinfo()
on it.