PHP code example of weew / url

1. Go to this page and download the library: Download weew/url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

weew / url example snippets


// protocol://username:[email protected]:port/path?key=value#fragment

// https://john:[email protected]:8080/my/path?query=value&some=value#hashtag

$url = new Url('http://username:[email protected]:80/some/path?query=value#fragment');

echo $url->getProtocol();
// http

echo $url->getHost();
// subdomain.domain.com

echo $url->getDomain();
// domain

echo $url->getSubdomain();
// subdomain

echo $url->getTLD();
// com

echo $url->getPort();
// 80

echo $url->getPath();
// /some/path

echo $url->getQuery();
// query=value

echo $url->getFragment();
// fragment

echo $url->getUsername();
// username

echo $url->getPassword();
// password

$url->setProtocol('https');

$url->setHost('another.domain.net');
// or
$url->setDomain('domain');
$url->setSubdomain('another');
$url->setTLD('net');

$url->setPort(8080);
$url->setPath('my/path');
$url->addPath('here');
$url->getQuery()->set('some', 'value');
$url->setFragment('hashtag');
$url->setUsername('john');
$url->setPassword('doe');

echo $url;
// https://john:[email protected]:8080/my/path/here?query=value&some=value#hashtag

$url = new Url('users/1');
// true
$url->match('users/{id}');

$url = new Url('users');
// false
$url->match('users/{id}');

$url = new Url('users/1');
// true
$url->match('users/{id?}');

$url = new Url('users');
// true
$url->match('users/{id?}');

$url = new Url('users/1');
// true
$url->match('users/{id}', [
    'id' => '[0-9]+',
]);

$url = new Url('users/abc');
// false
$url->match('users/{id}', [
    'id' => '[0-9]+',
]);

$url = new Url('users/1');
$dictionary = $url->parse('users/{id}');
// 1
$dictionary->get('id');

$url = new Url('{subdomain}.service.com/users/{id}/profile');
$url->replace('subdomain', 'api');
$url->replace('id', 1);

// or 
$url->replaceAll(['subdomain' => 'api', 'id' => 1]);

// api.service.com/users/1/profile
$url->toString();