wpscholar/url
Composer 安装命令:
composer require wpscholar/url
包简介
A PHP class for parsing and manipulating URLs.
README 文档
README
A PHP library for parsing, manipulating, and building URLs.
Description
This library provides a simple and intuitive way to work with URLs in PHP. It allows you to parse, manipulate, and build URLs while handling all the common URL components including scheme, host, port, path, query parameters, and fragments.
Features
- Parse URLs into their component parts
- Build URLs from component parts
- Add, remove, and modify query parameters
- Handle URL fragments
- Detect and manipulate trailing slashes
- Get current URL and scheme detection
- Path segment manipulation
- URL string conversion
Requirements
- PHP 7.2 or higher
Installation
Install via Composer:
composer require wpscholar/url
Testing
This library includes a comprehensive test suite built with PHPUnit. To run the tests:
- Install development dependencies:
composer install
- Run the test suite:
composer test
The test suite covers all major functionality including:
- URL parsing
- Query parameter manipulation
- Static helper methods
- Path manipulation
- URL output methods
The test suite is automatically run via GitHub Actions whenever code is pushed to the main branch or when a pull request is created. The tests are run against multiple PHP versions (7.2, 7.3, 7.4, 8.0, 8.1, 8.2, and 8.3) to ensure broad compatibility.
Basic Usage
use wpscholar\Url; // Create from a URL string $url = new Url('https://example.com/path?param=value#section'); // Get the current URL $currentUrl = new Url(); // Automatically uses current URL // Access URL components echo $url->scheme; // 'https' echo $url->host; // 'example.com' echo $url->path; // '/path' echo $url->query; // 'param=value' echo $url->fragment; // 'section' // Modify query parameters $url->addQueryVar('new_param', 'value'); $url->removeQueryVar('old_param'); // Get specific query parameter $value = $url->getQueryVar('param_name'); // Get all query parameters as array $params = $url->getQueryVars();
Static Helpers
// Get current URL $currentUrl = Url::getCurrentUrl(); // Get current scheme (http/https) $scheme = Url::getCurrentScheme(); // Strip query string from URL $cleanUrl = Url::stripQueryString($url); // Build URL from parts $url = Url::buildUrl([ 'scheme' => 'https', 'host' => 'example.com', 'path' => '/path', 'query' => 'param=value' ]);
Path Manipulation
// Given URL: https://example.com/blog/2023/post-title // Get all path segments as array $segments = $url->getSegments(); // Returns: ['blog', '2023', 'post-title'] // Get specific segment by index (zero-based) $year = $url->getSegment(1); // Returns: '2023' $section = $url->getSegment(0); // Returns: 'blog' $slug = $url->getSegment(2); // Returns: 'post-title'
URL Output
// Get full URL as string - multiple methods: $url = new Url('https://example.com/path?param=value'); // Method 1: Using toString() echo $url->toString(); // 'https://example.com/path?param=value' // Method 2: Cast to string directly echo (string) $url; // 'https://example.com/path?param=value' // Method 3: Using magic __toString() echo $url; // 'https://example.com/path?param=value' // Get URL parts as array $urlParts = $url->toArray(); // Returns array of URL components
wpscholar/url 适用场景与选型建议
wpscholar/url 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 260.08k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wpscholar/url 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wpscholar/url 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 260.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2016-03-31