PHP code example of zerig / url-parser

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

    

zerig / url-parser example snippets


$GLOBALS["server_root"] = new \UrlParser\Url("root");		// set root folder as ROOT
// BOTH variant are possile ↓
// during constructing URL obj, multiple slashes are transform to ONE
$url = new \UrlParser\Url(["http", "/www.web.cz/root", "/aaa/bbb", "a.html", "?member=me&age=15", "#hashtag"]);
$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb//a.html?member=me&age=15#hashtag");

// special possibility for TEMPORARY files
$url = new \UrlParser\Url('C:\xampp\tmp\php8C07.tmp', '\\');


$url->getScheme("string")   => "http"
$url->getHost("string")     => "www.web.cz"
$url->getRoot("string")     => "root"
$url->getPath("string")     => "aaa/bbb/a.html"
$url->getQuery("string")    => "?member=me&age=15"
$url->getFragment("string") => "hashtag"

makeItString("http://www.web.cz//aaa/bbb/a.html")
makeItString(["http", "/www.web.cz/root", "/aaa/bbb", "a.html"])

getString() => "http://www.web.cz/root/aaa/bbb/a.html"

$url = new \UrlParser\Url("http://www.web.cz//aaa/bbb/a.html");
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html"

$popped = $url->pop();
$url->getString() => "http://www.web.cz/root/aaa/bbb"
$popped => "a.html"

$popped = $url->pop(3);
$url->getString() => "http://www.web.cz"
$popped => [
	[0] => "bbb",
	[1] => "aaa"
]

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html"

$shifted = $url->shift();
$url->getString() => "http://www.web.cz/bbb/a.html"
$shifted => "aaa"

$shifted = $url->shift(3);
$url->getString() => "http://www.web.cz"
$shifted => [
	[0] => "bbb",
	[1] => "a.html"
]

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html"

$url->swap("aaa", "ccc");
$url->getString() => "http://www.web.cz/ccc/bbb/a.html"


"http://web.cz/aaa/bbb/c.html"

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html"

$url->getHost() => "www.web.cz"
$url->getHost("string") => "www.web.cz"
$url->getHost("array") => [
	[0] => "www.web.cz"
]

$url->getPath() => [
	[0] => "ccc",
	[1] => "bbb",
	[2] => "a.html"
]
$url->getPath("string") => "ccc/bbb/a.html"
$url->getPath("array") => [
	[0] => "ccc",
	[1] => "bbb",
	[2] => "a.html"
]


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->addPath("file");			// add PATH URL
$url->addQuery(["name" => "jerome"]);	// add Query in URL

$url->getString() => "http://www.web.cz/root/aaa/bbb/a/file.html?name=jerome"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->getDepth() => 2

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a");
$url->getDepth() => 3


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->linkRoot() => "../../"

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a");
$url->linkRoot() => "../../../"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->addPath(["ccc", "ddd"]);
$url->getPath("string") => "aaa/bbb/a/ccc/ddd.html"
$url->getString() => "http://www.web.cz/root/aaa/bbb/a/ccc/ddd.html"

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a");
$url->addPath(["ccc", "ddd"]);
$url->getPath("string") => "aaa/bbb/a/ccc/ddd"
$url->getString() => "http://www.web.cz/root/aaa/bbb/a/ccc/ddd"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->beforePath(["ccc", "ddd"]);

$url->getPath("string") => "ccc/ddd/aaa/bbb/a.html"
$url->getString() => "http://www.web.cz/root/ccc/ddd/aaa/bbb/a.html"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->addQuery(["name" => "jerome", "age=15"]);

$url->getQuery("string") => "?name=jerome&age=15"
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html?name=jerome&age=15"

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html?member=me");
$url->addQuery(["name" => "jerome", "age=15"]);

$url->getQuery("string") => "?member=me&name=jerome&age=15"
$url->getString() => "http://www.web.cz/root/aaa/bbb/a.html?member=me&name=jerome&age=15"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->hasExtension() => 1

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a");
$url->hasExtension() => 0


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->getExtension() => "html"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$extension = $url->removeExtension();

$url->getString() => "http://www.web.cz/root/aaa/bbb/a"
$extension => "html"


$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->exist() => 1

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->isFolder() => 0

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$url->isFile() => 1

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html?member=me&age=15");
$remove_part = $url->removeScheme();

$url->getString() => "www.web.cz/root/aaa/bbb/a.html"
$remove_part => "http"

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html");
$remove_part = $url->removePath(["aaa", "bbb"]);

$url->getString() => "www.web.cz/root/a.html"
$remove_part = [
	[0] => "aaa",
	[1] => "bbb"
]

$url = new \UrlParser\Url("http://www.web.cz/root/aaa/bbb/a.html?member=me&age=15");
$remove_part = $url->removeQuery(["member"]);

$url->getString() => "www.web.cz/root/aaa/bbb/a.html?age=15"
$remove_part = [
	["member"] => "me"
]