query-stringvsurl
The query-string library is a Node.js library that provides a simple way to parse and stringify query strings. It is useful for working with the query string portion of a URL, which is the part of the URL that follows 'the "?" character and contains key-value pairs.
url is a package for working, modifying and parsing web URLs. This package is for you when PHP's parse_url() is not enough.
Key Features:
- Parse a URL and access or modify all its components separately.
- Resolve any relative reference you may find in an HTML document to an absolute URL, based on the document's URL.
- Get not only the full host of a URL, but also the registrable domain, the domain suffix and the subdomain parts of the host separately (Thanks to the Mozilla Public Suffix List).
- An advanced API to access and manipulate the URL query component.
- Compare URLs or components of URLs (e.g. checking if different URLs point to the same host or domain)
- Thanks to symfony/polyfill-intl-idn it's also no problem to parse internationalized domain names (IDN).
- Includes an adapter class which implements the PSR-7 UriInterface.
Example Use
import queryString from 'query-string';
console.log(location.search);
//=> '?foo=bar'
const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}
console.log(location.hash);
//=> '#token=bada55cafe'
const parsedHash = queryString.parse(location.hash);
console.log(parsedHash);
//=> {token: 'bada55cafe'}
parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'
location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'
<?php
use Crwlr\Url;
$url = new Url('https://www.example.com/path?query=value#fragment');
// Parse url for parts:
echo $url->scheme();
echo $url->host();
echo $url->path();
echo $url->query();
echo $url->fragment();
// update values
echo $url->path("some/new/query");
// Create url from parts:
$url = new Url();
$url->scheme('https');
$url->host('www.example.com');
$url->path('/path');
$url->query('query=value');
$url->fragment('fragment');
echo $url->toString();