jeremyelliot/urlhelper

v1.1.1 2018-10-31 04:43 UTC

This package is auto-updated.

Last update: 2024-09-05 20:20:55 UTC


README

UrlHelper 是一个用于解析 URL 的包装类。它提供了一种方法来

  • 获取 URL 的特定部分
  • 将 URL 的部分提取到一个新的有效 URL 中。

UrlHelper 是不可变的。当 UrlHelper 转换为字符串时,它将给出与构造函数中传入相同的字符串。

使用 get() 方法从您拥有的部分构建 URL。使用 getContextPart() 方法将返回一个 URL 部分,Web 浏览器将在此部分后附加一个相对 URL。

示例

<?php

$fullUrl = new UrlHelper('https://boss:pass123@example.com/foo/bar.php?q=any&lang=en#baz');
$partialUrl = new UrlHelper('/foo/baz?param=value');

// this string is the pattern to get the full URL
$completeUrlPattern = 'scheme.user.pass.host.port.dir.file.ext.query.fragment';

echo $fullUrl->get($completeUrlPattern);
// --> https://boss:pass123@example.com/foo/bar.php?q=any&lang=en#baz
echo $partialUrl->get($completeUrlPattern);
// --> /foo/baz?param=value


echo $fullUrl->get('base');
// --> https://boss:pass123@example.com
echo $partialUrl->get('base');
// --> (empty UrlHelper, casts to empty string)


echo $fullUrl->get('user.host.dir');
// --> //boss@example.com/foo/
echo $partialUrl->get('user.host.dir');
// --> /foo/



echo $fullUrl->getContextPart();
// --> https://boss:pass123@example.com/foo/

echo $fullUrl->get('query');
// --> ?q=any&lang=en