Table of Contents
回复内容:
Home Backend Development PHP Tutorial html - php怎么获取页面上的超链接并补齐成全地址?

html - php怎么获取页面上的超链接并补齐成全地址?

Jun 06, 2016 pm 08:09 PM
html php

curl获取一个网站解析其中的a标签会得到链接,有的会是http:// 这样完整的,有的会是/about,../about相对的,有的是#,javascript之类的,怎么将匹配的链接全部补齐为完整链接(域名加相对的),锚点和js的排除掉?

回复内容:

curl获取一个网站解析其中的a标签会得到链接,有的会是http:// 这样完整的,有的会是/about,../about相对的,有的是#,javascript之类的,怎么将匹配的链接全部补齐为完整链接(域名加相对的),锚点和js的排除掉?

自己写个方法计算就行了。例如请求 http://example.com/qa/list.php, 其中主机地址是 http://example.com, 目录地址是 http://example.com/qa/
如果地址是 http(s)://开头,完整地址
如果地址是/开头, 如 /aboutus,完整地址是主机地址+该地址,即 http://example.com/aboutus
如果地址是其它开头,如 ../aboutus, 完整地址是目录地址+该地址,即 http://example.com/qa/../aboutus
如果你觉得../很碍眼,可以自己整理一下,每个../抵消一级父目录,变成 http://example.com/aboutus

/**
 * 返回当前请求的完整URL
 *
 * @return string
 */
function current_url()
{
    $host = $_SERVER['HTTP_HOST'];
    $uri = $_SERVER['REQUEST_URI'];

    return (is_https() ? 'https://' : 'http://') . $host . $uri;
}
Copy after login


好吧,看错题目了。。。

关于处理相对路径的问题可以参考我之前写过的一篇文章:http://blog.icewingcc.com/php-conv-addr-re-ab-2.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles