用于解析、构建和处理URL的辅助对象

1.0.3 2019-05-28 14:30 UTC

This package is auto-updated.

Last update: 2024-08-29 05:06:48 UTC


README

蛋白质 | URL

用于解析、构建和处理URL的辅助对象

安装

composer require proteins/url

通过

require the class 来使用类

use Proteins\URL;

解析URL

创建一个新的URL对象,将现有的URL作为构造函数参数传递,该URL将自动解析为URL组件。

$url = new URL('https://user:pass@www.alpha.beta.com:9080/path/to/resource.html?foo=bar&another[]=2&another[]=3#frag_link');

print_r($url);
Object
(
    [scheme] => https
    [user] => user
    [pass] => pass
    [host] => www.alpha.beta.com
    [port] => 9080
    [path] => /path/to/resource.html
    [query] => Array
        (
            [foo] => bar
            [another] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

    [fragment] => frag_link
)

URL类可以自动转换为构建的URL字符串

echo "$url";
https://user:pass@www.alpha.beta.com:9080/path/to/resource.html?foo=bar&another%5B0%5D=2&another%5B1%5D=3#frag_link

构建或修改URL

您可以修改或从头开始构建URL,通过修改单个组件

$url = new URL();

$url->host = 'caffeina.com';
$url->user = 'demo';

echo $url;
demo@caffeina.com
$url = new URL("ftps://test.com:9000/index.php");

$url->scheme = 'https';
$url->port = false;

echo $url;
https://test.com/index.php

示例

构建mailto地址

$link = new URL('mailto://');
$link->user = 'info';
$link->host = 'myserver.com';

$link->query['subject'] = 'This is a subject';
$link->query['body'] = 'Hi! This is a test... :D';

echo $link;
mailto://info@myserver.com?subject=This+is+a+subject&body=Hi%21+This+is+a+test...+%3AD