dsoihsnz85757 2016-09-25 08:04
浏览 89

PHP:获取页面的基本URL

Scenario:

User adds a URL (Like URLs below). I want to return all links on the page that appears in href attribute. I can get all links using DOMDocument class. But some links are relative. I want to convert them to absolute links.


My Problem:

Suppose I have these strings (URLs) (User may sends any link. like one of these):

http://example.com/test/index.html
http://example.com/test/test.php
http://example.com/test/
http://example.com/test

Now I get all contents of the href of user's input URL. Some of these URLs are relative so I have to merge those relative links to the base URL. My problem is that I cannot get the base URL. In this example my base URL would be http://example.com/test. So if user enters any of those URLs above I have to get the same base URL.

How can I extract the URL base correctly?

  • 写回答

1条回答 默认 最新

  • douduan6731 2016-09-25 09:08
    关注

    You can use parse_url method like this code:

    <?php 
        $url = 'http://example.com/test';
        //get base url from link
        $base_url=parse_url($url, PHP_URL_HOST);
        //get path from link
        $path=parse_url($url, PHP_URL_PATH);
        //print base_url
        echo 'Base URL = '.$base_url;
        //print path
        echo ' PATH = '.$path;
     ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀