PHP code example of simplon / url

1. Go to this page and download the library: Download simplon/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/ */

    

simplon / url example snippets


$url = new Url(
    'http://foo.bar.com/en/test/challenge?utm_source=source&utm_campaign=campaign&utm_medium=medium#hello-world'
);

$url->getProtocol(); // http
$url->getHost(); // foo.bar.com
$url->getSubDomain(); // foo
$url->getDomain(); // bar
$url->getTopLevelDomain(); // com
$url->getPath(); // /en/test/challenge
$url->getAllQueryParams(); // ['utm_source' => 'source', ...]
$url->getQueryParam('utm_source'); // source
$url->getPathSegment(1); // en
$url->getPathSegment(2); // test
$url->getFragment(); // hello-world

$url = new Url(
    'ftp://peter:[email protected]:21'
);

$url->getUser(); // peter
$url->getPass(); // sunny
$url->getPort(); // 21

$url = (new Url())
    ->withProtocol('https')
    ->withHost('dear-johnny.io')
    ->withPath('/foo/bar')
    ->withQueryParam('sun', 'is-shining')
    ->withQueryParam('training', 'yes')
    ->withFragment('hello-world');

echo $url; // https://dear-johnny.io/foo/bar?sun=is-shining&training=yes#hello-world

$url = new Url(
    'https://us.dear-johnny.io/foo/bar?sun=is-shining&training=yes#hello-world'
);

$url
    ->withoutSubDomain()
    ->withTopLevelDomain('com')
    ->withPathSegment(1, 'hoo')
    ->withPrefixPath('/en')
    ->withTrailPath('/much/more')
    ->withoutQueryParam('training')
    ->withQueryParam('sun', 'off')
    ->withoutFragment();

echo $url; // https://dear-johnny.com/en/hoo/bar/much/more?sun=off

//
// withPath
//

$route = '/say/{message}';
$url = new Url('https://foobar.io');
$url->withPath($route, ['message' => 'hello']);
echo $url; // https://foobar.io/say/hello

//
// withPrefixPath
//

$route = '/hello/{message}';
$url = new Url('https://foobar.io/bob');
$url->withPrefixPath($route, ['message' => 'there']);
echo $url; // https://foobar.io/hello/there/bob

//
// withTrailPath
//

$route = '/got/{count}/{item}';
$url = new Url('https://foobar.io/bob');
$url->withTrailPath($route, ['count' => 'five', 'item' => 'cars']);
echo $url; // https://foobar.io/bob/got/five/cars