league/uri-query-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

league/uri-query-parser

最新稳定版本:1.0.1

Composer 安装命令:

composer require league/uri-query-parser

包简介

parse and build a query string the right way in PHP

README 文档

README

Build Status Latest Version

THIS PACKAGE IS ON MAINTENANCE MODE SINCE 2019-10-18, FOR ANY NEW PROJECT PLEASE CONSIDER USING league/uri-components v2+

This package contains a userland PHP uri query parser and builder.

<?php

use League\Uri\Parser\QueryString;

$pairs = QueryString::parse('module=home&action=show&page=????');
// returns [
//     ['module', 'home'],
//     ['action', 'show'],
//     ['page', '????']
// ];

$str = QueryString::build($pairs, '|');
// returns 'module=home|action=show|page=????'

System Requirements

You need:

  • PHP >= 7.1.3 but the latest stable version of PHP is recommended

Installation

$ composer require league/uri-query-parser

Documentation

The parsing/building algorithms preserve pairs order and uses the same algorithm used by JavaScript UrlSearchParams

Parsing the URI query string

Parsing a query string is easy.

<?php

use League\Uri\Parser\QueryString;

$pairs = QueryString::parse('module=home&action=show&page=????');
// returns [
//     ['module', 'home'],
//     ['action', 'show'],
//     ['page', '????']
// ];

Description

<?php

public static function QueryString::parse($query, string $separator = '&', int $enc_type = PHP_QUERY_RFC3986): array;

The returned array is a collection of key/value pairs. Each pair is represented as an array where the first element is the pair key and the second element the pair value. While the pair key is always a string, the pair value can be a string or the null value.

The League\Uri\QueryString::parse parameters are

  • $query can be the null value, any scalar or object which is stringable;
  • $separator is a string; by default it is the & character;
  • $enc_type is one of PHP's constant PHP_QUERY_RFC3968 or PHP_QUERY_RFC1738 which represented the supported encoding algoritm

Here's a simple example showing how to use all the given parameters:

<?php

use League\Uri\QueryString;

$pairs = QueryString::parse(
    'module=home:action=show:page=toto+bar&action=hide',
    ':',
    PHP_QUERY_RFC1738
);
// returns [
//     ['module', 'home'],
//     ['action', 'show'],
//     ['page', 'toto bar'],
//     ['action', 'hide'],
// ];

Building the URI query string

To convert back the collection of key/value pairs into a valid query string or the null value you can use the QueryString::build function.

<?php

use League\Uri\Parser\QueryString;

$pairs = QueryString::build([
    ['module', 'home'],
    ['action', 'show'],
    ['page', 'toto bar'],
    ['action', 'hide'],
], '|', PHP_QUERY_RFC3986);

// returns 'module=home|action=show|page=toto%20bar|action=hide';

Description

<?php

public static function QueryString::build(iterable $pairs, string $separator = '&', int $enc_type = PHP_QUERY_RFC3986): ?string;

The QueryString::build :

  • accepts any iterable structure containing a collection of key/pair pairs as describe in the returned array of the QueryString::parse` function.

Just like with QueryString::parse, you can specify the separator and the encoding algorithm to use.

  • the function returns the null value if an empty array or collection is given as input.

Extracting PHP variables

<?php

public static function QueryString::extract($query, string $separator = '&', int $enc_type = PHP_QUERY_RFC3986): array;
public static function QueryString::convert(iterable $pairs): array;

QueryString::parse and QueryString::build preserve the query string pairs content and order. If you want to extract PHP variables from the query string à la parse_str you can use:

  • The QueryString::extract method which takes the same parameters as League\Uri\QueryString::parse
  • The QueryString::convert method which takes the result of League\Uri\QueryString::parse

both methods, however, do not allow parameters key mangling in the returned array like parse_str;

<?php

use League\Uri\Parser\QueryString;

$query = 'module=show&arr.test[1]=sid&arr test[4][two]=fred&+module+=hide';

$params = QueryString::extract($query, '&', PHP_QUERY_RFC1738);
// $params contains [
//     'module' = 'show',
//     'arr.test' => [
//         1 => 'sid',
//     ],
//     'arr test' => [
//         4 => [
//             'two' => 'fred',
//         ]
//     ],
//     ' module ' => 'hide',
// ];

parse_str($query, $variables);
// $variables contains [
//     'module' = 'show',
//     'arr_test' => [
//         1 => 'sid',
//         4 => [
//             'two' => 'fred',
//         ],
//     ],
//     'module_' = 'hide',
// ];

Exceptions

All exceptions extends the League\Uri\Parser\InvalidUriComponent marker class which extends PHP's InvalidArgumentException class.

  • If the query string is invalid a League\Uri\Exception\MalformedUriComponent exception is thrown.
  • If the query pair is invalid a League\Uri\Parser\InvalidQueryPair exception is thrown.
  • If the encoding algorithm is unknown or invalid a League\Uri\Parser\UnknownEncoding exception is thrown.
<?php

use League\Uri\Exception\InvalidUriComponent;
use League\Uri\Parser\QueryString;

try {
    QueryString::extract('foo=bar', '&', 42);
} catch (InvalidUriComponent $e) {
    //$e is an instanceof League\Uri\Parser\UnknownEncoding
}

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CONDUCT for details.

Testing

The library has a has a :

  • a PHPUnit test suite
  • a coding style compliance test suite using PHP CS Fixer.
  • a code analysis compliance test suite using PHPStan.

To run the tests, run the following command from the project folder.

$ composer test

Security

If you discover any security related issues, please email nyamsprod@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

league/uri-query-parser 适用场景与选型建议

league/uri-query-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 344.33k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「parser」 「url」 「components」 「query」 「uri」 「builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 league/uri-query-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 league/uri-query-parser 我们能提供哪些服务?
定制开发 / 二次开发

基于 league/uri-query-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 344.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 33
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04