PHP code example of mtchabok / url

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

    

mtchabok / url example snippets


use \Mtchabok\Url\Url;

$url = new Url('https://www.domain.com/dir1/dir2/file.ext?param1=val1&param2=val2');

$url = Url::newUrl('https://www.domain.com/dir1/dir2/file.ext?param1=val1&param2=val2');

$url = Url::newUrl()
	->setScheme('https')
	->setAuthority('www.domain.com')
	->setPath('/dir1/dir2/file.ext')
	->setQuery('param1=val1&param2=val2')
;


use \Mtchabok\Url\Url;
$url = Url::newUrl();
// change scheme
$url->setScheme('http'); // return $url object
$url->scheme = 'http';

// change authority = user: sam, pass: 123, host: domain.com
$url->setAuthority('sam:[email protected]'); // return $url object

// change url path.
$url->setPath('/dir2/dir1/'); // return $url object
$url->path = '/dir2/dir1/';

// add path
$url->addPath('./dir3/file.ext2'); // return $url object
$url->path = $url::normalizePath($url->path, './dir3/file.ext2');

// change query
$url->setQuery('p12=32&name=sara'); // return $url object
$url->query = 'p12=32&name=sara';

// set query param
$url->getQuery()->set('param1', 'test'); // return Url Query object
$url->query->set('param1', 'test'); // return Url Query object
$url->getQuery()->set('param4', ['p4_1'=>'val41', 'p4_2'=>'val42']); // return Url Query object
$url->query['param4'] = ['p4_1'=>'val41', 'p4_2'=>'val42']; // return Url Query object

// change fragment
$url->setFragment('index12'); // return $url object
$url->fragment = 'index12';


use \Mtchabok\Url\Url;
$url = Url::newUrl();
// check scheme set
$url->hasScheme(); // return bool

// check userInfo set
$url->hasUserInfo(); // return bool
$url->hasUser(); // return bool
$url->hasPass(); // return bool

// check host set
$url->hasHost(); // return bool

// check port set
$url->hasPort(); // return bool

// check path set
$url->hasPath(); // return bool

// check query set
$url->hasQuery(); // return bool

// and ...

use \Mtchabok\Url\Url;
$url = Url::newUrl();
// scheme
$url->getScheme(); // return current scheme or empty string
$url->scheme; // return current scheme or null
$url->getScheme('file'); // reutrn current scheme or 'file'

// user info
$url->getUserInfo(); // return current userinfo or empty string
$url->getUserInfo('test:123'); // return current userinfo or 'test:123'

// host
$url->getHost();  // return current host or empty string
$url->getHost('host.org');  // return current host or 'host.org'

// path
$url->getPath();  // return current path or empty string
$url->getPath('/dir1/dir2/file');  // return current path or '/dir1/dir2/file'


use \Mtchabok\Url\Url;
$url = Url::newUrl('https://www.domain.com/dir1/dir2/file.ext');

$url2 = $url->withHost('site.com'); // return new Url Object with host 'site.com'

$url->equalsHost($url2->getHost()); // return false
$url->equalsHost($url2); // return false