PHP code example of thinktomorrow / url

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

    

thinktomorrow / url example snippets


\Thinktomorrow\Url\Url::fromString('https://example.com');

// scheme
\Thinktomorrow\Url\Url::fromString('https://example.com')->getScheme(); // https

// host
\Thinktomorrow\Url\Url::fromString('https://example.com')->getHost(); // example.com

// port
\Thinktomorrow\Url\Url::fromString('https://example.com:9000')->getPort(); // 9000

// path
\Thinktomorrow\Url\Url::fromString('https://example.com/foo/bar')->getPath(); // foo/bar

// query
\Thinktomorrow\Url\Url::fromString('https://example.com?foo=bar')->getQuery(); // foo=bar

// hash
\Thinktomorrow\Url\Url::fromString('https://example.com#foobar')->getHash(); // foobar

Url::fromString('example.com')->secure()->get(); // 'https://example.com'

Url::fromString('example.com')->nonSecure()->get(); // 'http://example.com'
Url::fromString('https://example.com')->nonSecure()->get(); // 'http://example.com'

Url::fromString('http://example.com/foobar')
    ->setCustomRoot(Root::fromString('https://newroot.be'))
    ->get(); // 'https://newroot.be/foobar'

Url::fromString('http://example.com/foobar')
    ->localize('en')
    ->get(); // 'http://example.com/en/foobar'

Url::fromString('http://example.com/en/foobar')
    ->localize('fr', ['en','fr'])
    ->get(); // 'http://example.com/fr/foobar'

Url::fromString('http://example.com/en/foobar')
    ->localize(null, ['en','fr'])
    ->get(); // 'http://example.com/foobar'