Purl is a simple Object Oriented URL manipulation library for PHP 7.2+

Related tags

URL url php
Overview

Purl

Purl is a simple Object Oriented URL manipulation library for PHP 7.2+

Build Status Scrutinizer Quality Score Code Coverage Latest Stable Version Total Downloads

Installation

The suggested installation method is via composer:

composer require jwage/purl

Using Purl

Creating Url instances is easy. You can specify the URL you want, or just use the current URL:

use Purl\Url;

$url = new Url('http://jwage.com');
$currentUrl = Url::fromCurrent();

You can chain methods together after creating the Url like this:

$url = (new Url('http://jwage.com'))
    ->set('scheme', 'https')
    ->set('port', '443')
    ->set('user', 'jwage')
    ->set('pass', 'password')
    ->set('path', 'about/me')
    ->set('query', 'param1=value1&param2=value2')
    ->set('fragment', 'about/me?param1=value1&param2=value2');

echo $url->getUrl(); // https://jwage:password@jwage.com:443/about/me?param1=value1&param2=value2#about/me?param1=value1&param2=value2

// $url->path becomes instanceof Purl\Path
// $url->query becomes instanceof Purl\Query
// $url->fragment becomes instanceof Purl\Fragment

Path Manipulation

$url = new Url('http://jwage.com');

// add path segments one at a time
$url->path->add('about')->add('me');

// set the path data from a string
$url->path = 'about/me/another_segment'; // $url->path becomes instanceof Purl\Path

// get the path segments
print_r($url->path->getData()); // array('about', 'me', 'another_segment')

Query Manipulation

$url = new Url('http://jwage.com');
$url->query->set('param1', 'value1');
$url->query->set('param2', 'value2');

echo $url->query; // param1=value1&param2=value2
echo $url; // http://jwage.com?param1=value1&param2=value2

// set the query data from an array
$url->query->setData([
    'param1' => 'value1',
    'param2' => 'value2'
]);

// set the query data from a string
$url->query = 'param1=value1&param2=value2'; // $url->query becomes instanceof Purl\Query
print_r($url->query->getData()); //array('param1' => 'value1', 'param2' => 'value2')

Fragment Manipulation

$url = new Url('http://jwage.com');
$url->fragment = 'about/me?param1=value1&param2=value2'; // $url->fragment becomes instanceof Purl\Fragment

A Fragment is made of a path and a query and comes after the hashmark (#).

echo $url->fragment->path; // about/me
echo $url->fragment->query; // param1=value1&param2=value2
echo $url; // http://jwage.com#about/me?param1=value1&param2=value2

Extract URLs

You can easily extract urls from a string of text using the extract method:

$string = 'some text http://google.com http://jwage.com';
$urls = Url::extract($string);

echo $urls[0]; // http://google.com/
echo $urls[1]; // http://jwage.com/

Join URLs

You can easily join two URLs together using Purl:

$url = new Url('http://jwage.com/about?param=value#fragment');
$url->join('http://about.me/jwage');

echo $url; // http://about.me/jwage?param=value#fragment

Or if you have another Url object already:

$url1 = new Url('http://jwage.com/about?param=value#fragment');
$url2 = new Url('http://about.me/jwage');
$url1->join($url2);

echo $url1; // http://about.me/jwage?param=value#fragment
Comments
  • Adding support for

    Adding support for "protocol relative" / "schemeless" URLs

    ...or "network-path reference" as it's called by the RFC: http://tools.ietf.org/html/rfc3986#section-4.2

    Here's the related issue: https://github.com/jwage/purl/issues/13

    opened by MPV 22
  • Nz domains are not parsing correctly

    Nz domains are not parsing correctly

    I am using version 0.0.7 and tested this upto 0.0.9

    $url = \Purl\Url::parse('https://naren.nz/');
    echo $url->registerableDomain;
    //produces null
    //expected naren.nz
    
    $url = \Purl\Url::parse('https://www.naren.nz/');
    echo $url->registerableDomain;
    //produces "www.naren.nz"
    //expected naren.nz
    

    Please let me know if you need additional information.

    opened by developernaren 12
  • Purl cannot parse some Blogspot sudomains.

    Purl cannot parse some Blogspot sudomains.

    Hi,

    While those work fine:

    http://anil.blogspot.com.tr :: $purl->subdomain is "anil"
    http://anil.blogspot.com :: $purl->subdomain is "anil"
    

    Those domain extensions doesn't work:

    http://anil.blogspot.co.uk :: $purl->subdomain is NULL
    http://anil.blogspot.nl :: $purl->subdomain is NULL
    http://anil.blogspot.in :: $purl->subdomain is NULL
    

    Could be a bug.

    Is there any way I can extend domain extensions list for Blogspot and allow those domain extensions too?

    opened by Aristona 11
  • Feature request: building relative urls

    Feature request: building relative urls

    Library should be able to create relative urls:

    $url = new \Purl\Url('/events');
    $url->query->set('param1', 'value1');
    echo $url->getUrl();
    

    Expected output should be:

    /events?param1=value1
    
    opened by AidasK 9
  • Domain 'localhost' is stripped from input URL

    Domain 'localhost' is stripped from input URL

    Test case:

    <?php
    
    require_once __DIR__ . "/vendor/autoload.php";
    
    $url = new \Purl\Url('http://localhost/');
    echo $url . "\n";
    

    Expected output:

    http://localhost/
    

    Actual output:

    http:///
    

    This is using version 0.0.3.

    opened by jameshfisher 9
  • Fix seg fault in Query::doInitialize()

    Fix seg fault in Query::doInitialize()

    parse_str(), without a second argument, can cause php segfaults, as shown here: https://bugs.php.net/bug.php?id=73181

    This change avoids the segfault (because with a second argument, we avoid modifying the current scope), and also makes the code a bit cleaner.

    opened by arnaud-lb 6
  • Composer.json points at wrong version of php-domain-parser.

    Composer.json points at wrong version of php-domain-parser.

    Hello,

    I had to change the composer.json.

        "require": {
            "php": ">=5.3.0",
            "jeremykendall/php-domain-parser": "1.*"
        } 
    

    to

        "require": {
            "php": ">=5.3.0",
            "jeremykendall/php-domain-parser": "1.1.0"
        } 
    

    To install this. Version 1.2.0 does not seem to be compatible and the wildcard auto points at that one.

    Won't Fix 
    opened by yo-l1982 5
  • Add support for longer TLDs

    Add support for longer TLDs

    Please add support for longer TLDs. Currently, only two- and tree-letter TLDs are supported (such as .org, .cz, .de) but there are longer TLDs (e.g. .info, .name, .museum and so on) this time and many more ones will be confirmed by ICANN in the near future.

    opened by aikencz 5
  • require old jeremykendall/php-domain-parser version

    require old jeremykendall/php-domain-parser version

    I'm getting the following errors. This is because jwage/purl requires jeremykendall/php-domain-parser "^1.3.1". Could you change to allow a newer version please.

    PHP Deprecated: idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated in /var/www/vhosts/rubus.pineberry.com/vendor/jeremykendall/php-domain-parser/library/Pdp/Parser.php on line 209

    PHP Deprecated: idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated in /var/www/vhosts/rubus.pineberry.com/vendor/jeremykendall/php-domain-parser/library/Pdp/Parser.php on line 192

    opened by Smartsasse 4
  • Incompatible with modern software

    Incompatible with modern software

    Any software package with a dev dependency on PhpUnit 7+ is incompatible with jwage/purl:

    jwage/purl dev-master requires phpunit/phpunit ^4.8|^5.5|^6.5
    

    Conflicts with:

    Installation request for phpunit/phpunit ~7.0
    

    The problem is probably caused by phpunit being in require rather than require -dev.

    opened by Stratadox 4
  • Using parse() inside loop eats up all memory

    Using parse() inside loop eats up all memory

    I have a loop that runs around 75 times before all the memory on my system is allocated. Each loop purl consumes around 1.5mb by including the public suffix list. Is there a way around this?

    function convert($size)
    {
        $unit=array('b','kb','mb','gb','tb','pb');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
    }
    
    foreach ($domains as $domain)
    {
        echo \Purl\Url::parse($domain);
        // echo convert(memory_get_usage(true)); 
        // echo '<br />';
    }
    

    The memory usage output is below

    16 mb
    17.75 mb
    19.5 mb
    21 mb
    22.5 mb
    24 mb
    25.5 mb
    27 mb
    28.5 mb
    30 mb
    31.5 mb
    33 mb
    34.5 mb
    36 mb
    37.5 mb
    39 mb
    40.5 mb
    42 mb
    43.5 mb
    45 mb
    46.5 mb
    48 mb
    49.5 mb
    51 mb
    52.5 mb
    54 mb
    55.5 mb
    57 mb
    58.5 mb
    60 mb
    61.5 mb
    63 mb
    64.5 mb
    66 mb
    67.5 mb
    69 mb
    70.5 mb
    72 mb
    73.5 mb
    75 mb
    76.5 mb
    78 mb
    79.5 mb
    81 mb
    82.5 mb
    84 mb
    85.5 mb
    87 mb
    88.5 mb
    90 mb
    91.5 mb
    93 mb
    94.5 mb
    96 mb
    97.5 mb
    99 mb
    100.5 mb
    102 mb
    103.5 mb
    105 mb
    106.5 mb
    106.5 mb
    108 mb
    109.5 mb
    111 mb
    112.5 mb
    114 mb
    115.5 mb
    117 mb
    118.25 mb
    119.75 mb
    119.75 mb
    121.25 mb
    122.75 mb
    124.25 mb
    
    Bug 
    opened by GC-Max 4
  • Support PHP8

    Support PHP8

    Fixes #83

    doctrine/coding-standard ^6 and ^7 only supports PHP ^7.1, so updated to branch 8. (9 is also available.)

    Updated phpstan/phpstan + phpstan/phpstan-strict-rules from ^0.11 to ^0.12 to support PHP 8.

    Removed PHP 5 reference from composer.json

    Ran phpcbf, which fixed all CS fails as a result of updating.

    The two removed sniffs no longer exist (didnt look for the replacements, if any)

    __set magic method is only allowed to return void in PHP8: https://3v4l.org/uiGk3

    opened by dpi 4
  • Request: new patch version

    Request: new patch version

    This commit https://github.com/jwage/purl/commit/d1cf27f96a5889885677e58e313880bfb2717f83 was done after 1.0.0 which is quite confusing when upgradting form 0.0.10. So a new 1.0.1 might be useful

    opened by holtkamp 0
  • Purl can fail to parse the fragment

    Purl can fail to parse the fragment

    Purl tries to parse fragments, but does not check for errors. When it happens, the fragment is completely missing from the stringified URL:

    $url = new Url('http://example.com/#hello:123');
    echo (string) $url, "\n";
    

    This script outputs this:

    http://example.com/
    

    The problem is that in Fragment.php, $this->data is merged with the result of parse_url($this->fragment). If parse_url returns false, array_merge returns null, and $this->data is null:

    https://github.com/jwage/purl/blob/87bb71167c17f64abc59d41123672100f4c8b616/src/Purl/Fragment.php#L112

    opened by arnaud-lb 3
Releases(v1.0.0)
Owner
Jonathan H. Wage
Jonathan H. Wage
Laravel based API to shorten URLs and share them easily. Redirects to the real URL by entering a short URL generated by the API

URL Shortener Requirements: PHP 7.4 or above composer node / npm Installation clone the project from the Github repository, enter the project folder,

Julio Vergara 5 Nov 20, 2021
URI manipulation Library

URI The Uri package provides simple and intuitive classes to manage URIs in PHP. You will be able to parse, build and resolve URIs create URIs from di

The League of Extraordinary Packages 886 Jan 6, 2023
A simple URL shortener for PHP

Shorty Shorty is a simple URL shortener for PHP. Installation 1. Download and extract the files to your web directory. 2. Use the included database.sq

Mike Cao 210 Dec 19, 2022
A simple but powerful URL shortener

UrlShorter 这是一个足够简洁的Url短网址生成器 This is a simple Url shorter. 兼容性 在PHP7.X 与 PHP 8.0 下测试通过 安装 step 1: git clone git@github.com:soxft/UrlShorter.git step

xcsoft 74 Dec 21, 2022
A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain.

A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain. Table of Contents Full documentation Dock

null 1.7k Dec 29, 2022
URL shortener web application based on the Laravel PHP Framework.

UrlHub Warning: UrlHub is still in development, constantly being optimized and isn't still stable enough to be used in production environments. Whatev

Kei 349 Jan 4, 2023
The modern, privacy-aware URL Shortener built in PHP.

About UrlHum UrlHum is a modern, privacy-aware and fast URL Shortener built with PHP and the Laravel Framework. At the moment UrlHum is heavily under

UrlHum 622 Jan 7, 2023
URL shortener web application based on the Laravel PHP Framework.

UrlHub Warning: UrlHub is still in development, constantly being optimized and isn't still stable enough to be used in production environments. Whatev

Kei 348 Dec 23, 2022
:aerial_tramway: A modern, powerful, and robust URL shortener

?? A modern, minimalist, and lightweight URL shortener. Polr is an intrepid, self-hostable open-source link shortening web application with a robust A

Chaoyi Zha 4.6k Jan 1, 2023
A fast and powerful URL Shortener built with Laravel, VueJS, and Tailwind CSS.

A fast and powerful URL Shortener built with Laravel, VueJS, and Tailwind CSS.

Devpri 53 Dec 25, 2022
Simpler Url Shortener for Laravel

Laravel Url Shortener Install composer require magarrent/laravel-url-shortener Run migrations: php artisan migrate Configuration If you want to config

Marc Garcia Torrent 51 Dec 17, 2022
🔗 Your Own URL Shortener

Your Own URL Shortener YOURLS is a set of PHP scripts that will allow you to run Your Own URL Shortener, on your server. You'll have full control over

YOURLS 8.8k Jan 3, 2023
A URL shortener with various other utilities, backed by a custom lightweight framework.

da.gd What is da.gd? da.gd is both a URL shortener and a collection of quick-info tools written in PHP. It allows you to use curl (or any http client)

da.gd 76 Jan 2, 2023
Laravel URL Localization Manager - [ccTLD, sub-domain, sub-directory].

Laravel URL Localization - (ccTLD, sub-domain, sub-directory). with Simple & Easy Helpers. Afrikaans Akan shqip አማርኛ العربية հայերեն অসমীয়া azərbayca

Pharaonic 2 Aug 7, 2022
Enables the possibility generating sanitized URL parts from persisted patterns.

#Persisted sanitized pattern mapping What does it do? Enables the possibility generating sanitized URL parts from persisted patterns. How does it work

Markus Hofmann 1 Apr 7, 2022
Checking an arbitrary URL for Micro-Framework HLEB

Checking an arbitrary URL for Micro-Framework HLEB The class RouteFinder is not included in the original configuration of the framework HLEB, so it mu

Foma Tuturov 1 Nov 4, 2021
URL - link shortener based on sqlite

link-url-shortener url - link shortener based on sqlite.

Okin On 1 Nov 12, 2021
A modern, powerful, and robust URL shortener

?? A modern, minimalist, and lightweight URL shortener. Polr is an intrepid, self-hostable open-source link shortening web application with a robust A

Chaoyi Zha 4.6k Dec 30, 2022
A simple PHP library to parse and manipulate URLs

Url is a simple library to ease creating and managing Urls in PHP.

The League of Extraordinary Packages 351 Dec 30, 2022