PHP:如何将尾部斜杠添加到绝对 URL

编程入门 行业动态 更新时间:2024-10-11 11:18:36
本文介绍了PHP:如何将尾部斜杠添加到绝对 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个绝对 URL 列表.我需要确保它们都有斜杠(如适用).所以:

I have a list of absolute URLs. I need to make sure that they all have trailing slashes, as applicable. So:

http://www.domain/ <-- 不需要尾部斜杠http://www.domain <-- 需要尾部斜杠http://www.domain/index.php <-- 不需要尾部斜杠http://www.domain/?message=hello <-- 不需要尾部斜线 http://www.domain/ <-- does not need a trailing slash http://www.domain <-- needs a trailing slash http://www.domain/index.php <-- does not need a trailing slash http://www.domain/?message=hello <-- does not need a trailing slash

我猜我需要使用正则表达式,但匹配 URL 很痛苦.希望有更简单的解决方案.想法?

I'm guessing I need to use regex, but matching URLs are a pain. Was hoping for an easier solution. Ideas?

推荐答案

您可以使用 parse_url() 来做到这一点.例如:

Rather than doing this using regex, you could use parse_url() to do this. For example:

$url = parse_url("http://www.example/ab/abc.html?a=b#xyz");
if(!isset($url['path'])) $url['path'] = '/';
$surl = $url['scheme']."://".$url['host'].$url['path'].'?'.$url['query'].'#'.$url['fragment'];
echo $surl;

这篇关于PHP:如何将尾部斜杠添加到绝对 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 14:31:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1174862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   如何将   PHP   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!