PHP code example of iceylan / urlify

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

    

iceylan / urlify example snippets




use Iceylan\Urlify\Url;
use Iceylan\Urlify\Query\Query;
use Iceylan\Urlify\Fragment;

use Iceylan\Urlify\Url;
use Iceylan\Urlify\Query\Query;

// The URL we're working with
$url = new Url(
    'https://example.com/something/utm_medium=target:readme|foo:bar&utm_source=github/its-me'
);

// Get the dataset as a Query object (we can use negative indexes)
$segmentAsQuery = $url->path->getSegmentAsQuery( -2 );

// Extract the utm_medium value as a Query object
$mediumAsQuery = $segmentAsQuery->getAsQuery( 'utm_medium', '|', ':' );

// access the target key's value
echo $mediumAsQuery->get( 'target' ); // "readme"

echo $url
        ->path
        ->getSegmentAsQuery( -2 )
        ->getAsQuery( 'utm_medium', '|', ':' )
        ->get( 'target' );
// readme

use Iceylan\Urlify\Url;

$url = new Url( 'https://user:[email protected]:8080/users//foo/../profile?view=full#section1' );

$url->scheme;
$url->auth;
$url->host;
$url->port;
$url->path;
$url->query;
$url->fragment;

echo $url
    ->setHost( 'iceylan.dev' )
    ->setScheme( 'http' )
    ->setPort( 3000 )
    ->setFragment( 'new-section' );

// Output: http://iceylan.dev:3000/users/profile?view=full#new-section

echo $url; // Outputs: https://example.com:8080/users/profile?view=full#section1

var_dump( $url->toArray());

// Outputs:
// [
// 	'scheme'   => 'https://',
// 	'user'     => 'user',
// 	'pass'     => 'pass',
// 	'host'     => 'example.com',
// 	'port'     => ':8080',
// 	'path'     => '/user/profile',
// 	'query'    => '?view=full',
// 	'fragment' => '#section1',
// ]

echo json_encode( $url );

use Iceylan\Urlify\Url;

echo ( new Url )
    ->setScheme( 'ws' )
    ->setHost( 'example.com' )
    ->setPath( '/users/profile' )
    ->setQuery( 'view=full&flag' )
    ->setFragment( 'section1' );

// Outputs: wa://example.com/users/profile?view=full&flag#section1

use Iceylan\Urlify\Url;

$url = ( new Url )
    ->setScheme( 'ws' )
    ->setHost( 'example.com' );

$url->path
    ->append( 'profile' )
    ->prepend( 'users' );

echo $url;
// Outputs: ws://example.com/users/profile

$url = ( new Url )
    ->setScheme( 'ws' )
    ->setHost( 'example.com' )
    ->buildPath( fn ( $path ) =>
        $path
            ->append( 'profile' )
            ->prepend( 'users' )
    )
    ->setQuery( 'view=full&flag' );

echo $url;
// Outputs: ws://example.com/users/profile?view=full&flag

use Iceylan\Urlify\Url;

$scheme = ( new Url( 'https://example.com' ))->scheme;

use Iceylan\Urlify\Scheme;

$scheme = new Scheme( 'https' );

$scheme = new Scheme;

$scheme->get(); // 'https'
(string) $scheme; // 'https://'

echo $scheme->set( 'tel' ); // 'tel:'

echo $url->setScheme( 'sms' ); // 'sms:'

echo $scheme->clean(); // ''

echo $scheme->set( null ); // ''
echo $url->setScheme( null ); // ''

$scheme->set( 'ftp' )->isSecure(); // false
$scheme->set( 'ftps' )->isSecure(); // true

$scheme->set( 'mysql' )->isKnown(); // true
$scheme->set( 'asgardia' )->isKnown(); // false

Scheme::registerScheme( name: 'asgardia', suffix: '://', secure: true );

$scheme->set( 'asgardia' );

$scheme->isKnown(); // true
$scheme->isSecure(); // true

echo $scheme; // 'asgardia://'

json_encode( $scheme );

use Iceylan\Urlify\Url;

$auth = ( new Url( 'https://username:[email protected]' ))->auth;

use Iceylan\Urlify\Auth;

$auth = new Auth( 'username', 'password' );

$auth = new Auth;

$auth->getUser(); // 'username'
$auth->getPass(); // 'password'

// You can set simultaneously
$auth->set( 'username', 'password' );

// Or separately
$auth->setUser( 'root' );
$auth->setPass( '1234' );

echo $auth; // 'root:1234@'

echo $url->auth->set( 'username', null ); // 'username@'
echo $url->auth->set( null, 'password' ); // ':password@'

$auth->clean();

// equivalent to clean method
$auth->set( null, null );

// clear separately
$auth->setUser( null );
$auth->setPass( null );

// echo triggers Url::__toString method
echo $url->setUsername( null )->setPassword( null ); // ''

$auth->isEmpty(); // true or false

json_encode( $auth );

use Iceylan\Urlify\Url;

$host = ( new Url( 'https://example.com' ))->host;

use Iceylan\Urlify\Host;

$host = new Host( 'www.foo.example.co.uk' );

$host = new Host;

$host->getSubdomainName();      // www.foo
$host->getSubdomains();         // ['www', 'foo']
$host->getPrimaryDomainName();  // example
$host->getTopLevelDomainName(); // co.uk
$host->getRootDomainName();     // example.co.uk

echo $host->set( 'subdomain.example.com' );
// 'subdomain.example.com'

echo $url->setHost( 'api.example.com' );
// 'https://api.example.com'

echo $host->setSubdomain( null ); // example.com
echo $host->setSubdomain( 'bar.baz' ); // bar.baz.example.com

echo $host->appendSubdomain( 'zoo' ); // bar.baz.zoo.example.com
echo $host->prependSubdomain( 'chat' ); // chat.bar.baz.zoo.example.com

echo $host->setPrimaryDomainName( 'exam' ); // chat.bar.baz.zoo.exam.com

echo $host->setTopLevelDomainName( 'co.uk' ); // chat.bar.baz.zoo.exam.co.uk

echo $host->clean(); // ''
// or
echo $host->set( null ); // ''

json_encode( $host );

use Iceylan\Urlify\Url;

$port = ( new Url( 'https://example.com:8001' ))->port;

use Iceylan\Urlify\Port;

$port = new Port( 8001 );

$port = new Port;

$port->get(); // 8001

echo $port->set( 8001 ); // ':8001'

echo $port->isEmpty(); // false

echo $port->getEffective(); // 8001

use Iceylan\Urlify\Port;

echo Port::getDefaultPortForScheme( 'https' ); // 443

echo $port->clean(); // ''
// or
echo $port->set( null ); // ''

json_encode( $port );