如何将域名后的所有内容都变成字符串

How to get everything after the domain name into a string(如何将域名后的所有内容都变成字符串)
本文介绍了如何将域名后的所有内容都变成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的目标是将域名后的所有内容都变成一个字符串.就像在 mysite.com/page/page2 中一样,会产生一个字符串page/page2".我可以做到,但是它开始给我带来问题,例如,当站点位于子文件夹中而不是根目录中时,站点所在的文件夹也将包含在字符串中,如果我不使用 mod_rewrite获得漂亮的链接,它还会将 index.php 添加到字符串中.

因此,我需要一两个技巧来让脚本了解该站点是否位于诸如 mysite.com/sitefolder/page/page2 之类的子文件夹中,并且它仍然会产生一个字符串

page/page2

如果站点没有使用mod_rewrite并且url是mysite.com/sitefolder/index.php/page/page2,它仍然会产生一个字符串

page/page2

请记住,我在配置文件中定义了 URL 和 USE_MOD_REWRITE,所以不需要魔法.我只是不知道如何获取该字符串.我知道我可以执行 $_SERVER['REQUEST_URI'] 来获取字符串,但是 index.php 仍然会在其中.如果我解释得不够好,我很抱歉,但感谢所有帮助.

解决方案

  • 解析请求uri获取路径
  • 从字符串的末尾删除最终的脚本名称(例如 index.php)
  • 删除任何尾部斜杠

    <前>$request = parse_url($_SERVER['REQUEST_URI']);$path = $request["path"];$result = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');

编辑

$request = parse_url($_SERVER['REQUEST_URI']);$path = $request["path"];$result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');$result =explode('/', $result);$max_level = 2;而 ($max_level < count($result)) {未设置($result[0]);}$result = '/'.implode('/', $result);

My goal is to get everything after the domain name into a string. As in mysite.com/page/page2 would result in a string "page/page2". That I can do, however it starts giving me problems when, for example, the site is in a subfolder and not in root, then the folder the site is in will also be included in the string and if I'm not using mod_rewrite to get pretty links, it will also add index.php to the string.

So, I would need a trick or two to make the script understand whether or not the site is in a subfolder such as mysite.com/sitefolder/page/page2 and that it would still result in a string

page/page2

If the site doesn not use mod_rewrite and the url is mysite.com/sitefolder/index.php/page/page2, it would still result in a string

page/page2

Keep in mind that I have URL and USE_MOD_REWRITE defined in a config file, so no need for magic. I just have no idea how to go about getting that string. I know I could do $_SERVER['REQUEST_URI'] to get the string, but then index.php would still be in it. I'm sorry if I didn't explain well enough, but all help is appreciated.

解决方案

  • get the path by parsing the request uri
  • remove eventual script name from the end of the string (e.g. index.php)
  • rtrim any trailing slashes

    $request = parse_url($_SERVER['REQUEST_URI']);
    $path = $request["path"];
    $result = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
    

EDIT

$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];

$result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');

$result = explode('/', $result);
$max_level = 2;
while ($max_level < count($result)) {
    unset($result[0]);
}
$result = '/'.implode('/', $result);

这篇关于如何将域名后的所有内容都变成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)