All Projects → thephpleague → uri-query-parser

thephpleague / uri-query-parser

Licence: MIT license
a parser and a builder to work with URI query string the right way in PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to uri-query-parser

querqy-elasticsearch
Querqy for Elasticsearch
Stars: ✭ 37 (-2.63%)
Mutual labels:  query, query-builder, query-parser
Api Query Params
Convert URL query parameters to MongoDB queries
Stars: ✭ 141 (+271.05%)
Mutual labels:  url, query-builder
Linkt
A lightweight and simple Kotlin library for deep link handling on Android 🔗.
Stars: ✭ 101 (+165.79%)
Mutual labels:  url, uri
Scala Uri
Simple scala library for building and parsing URIs
Stars: ✭ 225 (+492.11%)
Mutual labels:  url, uri
Uri.js
Javascript URL mutation library
Stars: ✭ 6,119 (+16002.63%)
Mutual labels:  url, uri
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+2376.32%)
Mutual labels:  url, uri
Vscode Remote Workspace
Multi protocol support for handling remote files like local ones in Visual Studio Code.
Stars: ✭ 197 (+418.42%)
Mutual labels:  url, uri
UrlCombine
C# util for combining Url paths. Works similarly to Path.Combine.
Stars: ✭ 23 (-39.47%)
Mutual labels:  url, uri
Uri Components
League URI components objects
Stars: ✭ 244 (+542.11%)
Mutual labels:  url, uri
ST-OpenUri
The ultimate Sublime Text plugin for opening URIs (URLs) in your file.
Stars: ✭ 25 (-34.21%)
Mutual labels:  url, uri
url-normalize
URL normalization for Python
Stars: ✭ 82 (+115.79%)
Mutual labels:  url, uri
Uri Parser
RFC3986/RFC3987 compliant URI parser
Stars: ✭ 342 (+800%)
Mutual labels:  url, uri
Searchwithmybrowser
Open Cortana searches with your default browser.
Stars: ✭ 285 (+650%)
Mutual labels:  url, uri
Use Query Params
React Hook for managing state in URL query parameters with easy serialization.
Stars: ✭ 1,278 (+3263.16%)
Mutual labels:  url, query
Uri
🌏 Functions for making sense out of URIs in PHP
Stars: ✭ 259 (+581.58%)
Mutual labels:  url, uri
Tldts
JavaScript Library to work against complex domain names, subdomains and URIs.
Stars: ✭ 151 (+297.37%)
Mutual labels:  url, uri
ocaml-uri
RFC3986 URI parsing library for OCaml
Stars: ✭ 85 (+123.68%)
Mutual labels:  url, uri
URLQueryItemEncoder
A Swift Encoder for encoding any Encodable value into an array of URLQueryItem.
Stars: ✭ 60 (+57.89%)
Mutual labels:  url, query
Pguri
uri type for PostgreSQL
Stars: ✭ 235 (+518.42%)
Mutual labels:  url, uri
uri
A type to represent, query, and manipulate a Uniform Resource Identifier.
Stars: ✭ 16 (-57.89%)
Mutual labels:  url, uri

Uri Query Parser

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 [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].