Documentation for crwlr / url (v0.1)

Attention: You're currently viewing the documentation for v0.1 of the url package.
This is not the latest version of the package.
If you didn't navigate to this version intentionally, you can click here to switch to the latest version.

Getting Started

The Swiss Army Knife for 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).
  • Compare urls or components of urls (e.g. checking if different urls point to the same host or domain)
  • Thanks to true/punycode it's also no problem to parse internationalized domain names (IDN).
  • Implements PSR-7 UriInterface.

Installation

Install the latest version with:

composer require crwlr/url

Usage

Including the package

<?php

include('vendor/autoload.php');

use Crwlr\Url\Url;

To start using the library include composer's autoload file and import the Url class so you don't have to write the full namespace path again and again. Further code examples skip the above.

Parsing urls

$url = Url::parse('https://john:123@www.example.com:8080/foo?bar=baz');

// Accessing url components via method calls
$port = $url->port();                   // => 8080
$domainSuffix = $url->domainSuffix();   // => "com"
$path = $url->path();                   // => "/foo"
$fragment = $url->fragment();           // => NULL

// Or as properties
$scheme = $url->scheme;                 // => "https"
$user = $url->user;                     // => "john"
$host = $url->host;                     // => "www.example.com"
$domain = $url->domain;                 // => "example.com"