PHP 库,用于访问 URL 信息。

资助包维护!
Josantonius

v2.0.1 2022-09-29 21:29 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:01 UTC


README

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12

翻译: 西班牙语

PHP 库,用于访问 URL 信息。

提供对 PHP 的 parse_urlpathinfo 函数提供的 URL 组件访问的改进替代方案。

此库不会格式化提供的 URL,它只是使其更容易访问组件。对于更高级的功能,您可以使用类似 league/uri-components 的工具。

要求

  • 操作系统:Linux | Windows。

  • PHP 版本:8.1 | 8.2。

安装

通过 Composer 安装此扩展是首选方式。

要安装 PHP URL 库,只需

composer require josantonius/url

前面的命令只会安装必要的文件,如果您想 下载整个源代码,可以使用

composer require josantonius/url --prefer-source

您还可以使用 Git 克隆完整的存储库

git clone https://github.com/josantonius/php-url.git

可用类

Url 类

Josantonius\Url\Url

创建一个新的实例

/**
 * If no URL is provided, the URL of the current page will be generated.
 * 
 * The generated URL will exclude ports 80 and 443 and include the rest.
 */
public function __construct(null|string $url = null);

获取权威

/**
 * The authority, in "[user-info@][host][:port]" format.
 *
 * @var string URL authority or empty string.
 */
public readonly string $authority;

获取基本 URL

/**
 * The base URL, in "[scheme:][//domain][:port]" format.
 *
 * @var string Base URL or empty string.
 */
public readonly string $base;

获取路径基本名

/**
 * The path basename, in "[filename][.extension]" format.
 *
 * @var string URL path basename or empty string.
 */
public readonly string $basename;

获取路径目录名

/**
 * The path dirname, in "[dirname]" format.
 *
 * @var string URL path dirname or empty string.
 */
public readonly string $dirname;

获取路径基本名扩展

/**
 * The path basename extension, in "[extension]" format.
 *
 * @var string URL path basename extension or empty string.
 */
public readonly string $extension;

获取路径文件名

/**
 * The path filename, in "[filename]" format.
 *
 * @var string URL path filename or empty string.
 */
public readonly string $filename;

获取片段

/**
 * URL fragment in "[fragment]" format.
 *
 * @var string URL fragment or empty string.
 */
public readonly string $fragment;

获取完整 URL

public readonly string $full;

获取散列片段

/**
 * URL hashed fragment in "[#fragment]" format.
 *
 * @var string URL hashed fragment or empty string.
 */
public readonly string $hash;

获取主机

/**
 * URL host in "[subdomain.][domain][.tld]" format.
 *
 * @var string URL host or empty string.
 */
public readonly string $host;

获取路径

/**
 * URL path in "[path]" format.
 *
 * @var string URL path or empty string.
 */
public readonly string $path;

获取查询参数

/**
 * URL query parameters in array format.
 *
 * @var array<string, mixed> URL query parameters or empty string.
 */
public readonly array $parameters;

获取密码

/**
 * URL password in "[password]" format.
 *
 * @var string URL password or empty string.
 */
public readonly string $password;

获取端口

/**
 * URL port in "[port]" format.
 *
 * @var string URL port or empty string.
 */
public readonly int|string $port;

获取方案

/**
 * URL scheme in "[scheme]" format.
 *
 * @var string URL scheme or empty string.
 */
public readonly string $scheme;

获取路径段

/**
 * URL path segments in array format.
 *
 * @var string[] URL path segments or empty string.
 */
public readonly array $segments;

获取查询

/**
 * URL query in "[query]" format.
 *
 * @var string URL query or empty string.
 */
public readonly string $query;

获取用户名

/**
 * URL username in "[username]" format.
 *
 * @var string URL username or empty string.
 */
public readonly string $username;

用法

此库的用法示例

使用当前 URL 创建新实例

use Josantonius\Url\Url;

$url = new Url();

使用自定义 URL 创建新实例

use Josantonius\Url\Url;

$url = new Url('https://domain.com');

获取权威

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com

$url->authority;  // "domain.com"


$url = new Url('https://user:pass@sub.domain.com:90/en/');

$url->authority; // "user:pass@sub.domain.com:90"


$url = new Url('https://user:pass@sub.domain.com/en/');

$url->authority; // "user:pass@sub.domain.com"


$url = new Url('https://sub.domain.com/en/');

$url->authority; // "sub.domain.com"

获取基本 URL

use Josantonius\Url\Url;

$url = new Url(); // https://user:pass@domain.com:80/en/

$url->base; // "https://domain.com"


$url = new Url('https://domain.com:80/?tag=bug');

$url->base; // "https://domain.com:80"


$url = new Url('https://domain.com/en/');

$url->base; // "https://domain.com"

获取路径基本名

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->basename; // "search.php"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->basename; // "search.php"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->basename; // "docs"

获取路径目录名

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->dirname; // "/"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->dirname; // "/en/web/docs"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->dirname; // "/en/web"

获取路径基本名扩展

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->extension; // "php"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->extension; // "php"


$url = new Url('https://domain.com/en/web/docs?tag=bug');

$url->extension; // ""

获取路径文件名

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/search.php

$url->filename; // "search"


$url = new Url('https://domain.com/en/web/docs/search.php?tag=bug');

$url->filename; // "search"


$url = new Url('https://domain.com/docs?tag=bug');

$url->filename; // "docs"

获取片段

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com#top

$url->fragment; // "top"


$url = new Url('https://domain.com/en/web/docs#top');

$url->fragment; // "top"


$url = new Url('https://domain.com');

$url->fragment; // ""

获取完整 URL

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com:80

$url->full;  // "https://domain.com"


$url = new Url('https://user:pass@sub.domain.com:90/en/');

$url->full; // "https://user:pass@sub.domain.com:90/en/"

获取散列片段

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com#top

$url->hash; // "#top"


$url = new Url('https://domain.com/en/web/docs#top');

$url->hash; // "#top"


$url = new Url('https://domain.com');

$url->hash; // ""

获取主机

use Josantonius\Url\Url;

$url = new Url(); // https://sub.domain.com

$url->host; // "sub.domain.com"


$url = new Url('https://sub.domain.com/en/web/docs#top');

$url->host; // "sub.domain.com"


$url = new Url('https://domain.com');

$url->host; // "domain.com"


$url = new Url('https://localhost');

$url->host; // "localhost"

获取路径

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/en

$url->path; // "/en/web/docs/search.php"


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->path; // "/en/web/docs/search.php"


$url = new Url('https://domain.com/en/web/docs/');

$url->path; // "/en/web/docs/"


$url = new Url('https://domain.com/en?tag=bug');

$url->path; // "/en"


$url = new Url('https://domain.com?tag=bug');

$url->path; // ""

获取查询参数

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com/en?tag=bug&order=asc#top

$url->parameters; // ["tag" => "bug", "order" => "asc"]


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->parameters; // ""

获取密码

use Josantonius\Url\Url;

$url = new Url(); // https://:pass@domain.com

$url->password; // "pass"


$url = new Url('https://user:pass@domain.com');

$url->password; // "pass"


$url = new Url('https://user@domain.com');

$url->password; // ""

获取端口

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com:90

$url->port; // 90


$url = new Url(); // https://domain.com:80

$url->port; // ""


$url = new Url(); // https://domain.com:443

$url->port; // ""


$url = new Url('https://domain.com:80/en/');

$url->port; // 80


$url = new Url('https://domain.com:443/en/');

$url->port; // 443


$url = new Url('https://domain.com/en/');

$url->port; // ""

获取方案

use Josantonius\Url\Url;

$url = new Url(); // http://domain.com

$url->scheme; // "http"


$url = new Url('https://domain.com');

$url->scheme; // "https"

获取路径段

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com?tag=bug

$url->segments; // []


$url = new Url('https://domain.com/en/web/docs/search.php');

$url->segments; // ['en', 'web', 'docs', 'search.php']

获取查询

use Josantonius\Url\Url;

$url = new Url(); // https://domain.com?tag=bug

$url->query; // "tag=bug"


$url = new Url('https://domain.com?tag=bug&order=asc#top');

$url->query; // "tag=bug&order=asc"

$url = new Url('https://domain.com');

$url->query; // ""

获取用户名

use Josantonius\Url\Url;

$url = new Url(); // https://user@domain.com

$url->username; // "user"


$url = new Url('https://:pass@domain.com');

$url->username; // ""


$url = new Url('https://user:pass@domain.com');

$url->username; // "user"


$url = new Url('https://domain.com');

$url->username; // ""

测试

要运行 测试,您只需要 composer 并执行以下命令

git clone https://github.com/josantonius/php-url.git
cd php-url
composer install

使用 PHPUnit 运行单元测试

composer phpunit

使用 PHPCS 运行代码标准测试

composer phpcs

运行 PHP Mess Detector 测试以检测代码风格的不一致性

composer phpmd

运行所有之前的测试

composer tests

待办事项

  • 添加新功能
  • 改进测试
  • 改进文档
  • 改进 README 文件中的英文翻译
  • 重构代码以禁用代码风格规则(请参阅 phpmd.xml 和 phpcs.xml)

变更日志

每个版本的详细更改记录在 发布说明 中。

贡献

在发起拉取请求、开始讨论或报告问题之前,请务必阅读 贡献指南

感谢所有 贡献者!❤️

赞助

如果此项目帮助您减少了开发时间,您可以 赞助我 以支持我的开源工作 😊

许可证

本存储库遵循MIT许可证

版权所有 © 2017-至今,Josantonius