PHP Classes

URL Utility: Compose an URL from its component definitions

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 49%Total: 584 All time: 5,298 This week: 140Up
Version License PHP version Categories
urlutility 1.0GNU Lesser Genera...7Networking, Text processing
Description 

Author

This class can be used to compose an URL from its component definitions.

It can take as parameters the URL protocol scheme, the host, the port, the path and the query parameters. Then it generates the complete URL from the specified parameters.

The class change, get, remove the URL parameters individually. It can also merge parameters of different URLs.

Picture of Marcel Oehler
Name: Marcel Oehler <contact>
Classes: 1 package by
Country: Switzerland Switzerland

Example

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>URLUtility</title>
</head>
<body>
<pre>
<?php

require('../library/URLUtility.php');
$link = new URLUtility();

echo
'Current document:' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setScheme('https');
$link->setHost('local_host');
$link->setPort('4242');
$link->setPath('/any/path/');
$link->setFileName('script_file.php');

echo
'setScheme(\'https\')' . PHP_EOL . 'setHost(\'local_host\')' . PHP_EOL . 'setPort(\'4242\')' . PHP_EOL . 'setPath(\'/any/path/\')' . PHP_EOL . 'setFileName(\'script_file.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

// --- SET ---

$link->setQuery('one=[1]&two=[2]&three=[3]');
echo
'setQuery(\'one=[1]&two=[2]&three=[3]\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setFilePath('/path/to/script.php');
echo
'setFilePath(\'/path/to/script.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setFilePathQuery('/path/to/another/script.php?four=[4]&five=[5]');
echo
'setFilePathQuery(\'/path/to/another/script.php?four=[4]&five=[5]\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setSchemeHost('http://www.local-host.ch');
echo
'setSchemeHost(\'http://www.local-host.ch\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setSchemeHostPath('http://www.local_host.ch');
echo
'setSchemeHostPath(\'http://www.local_host.ch\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->setSchemeHostFilePath('http://www.localhost.ch');
echo
'setSchemeHostFilePath(\'http://www.localhost.ch\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

// --- MERGE ---

$link->mergeQuery('one=[1]&two=[2]&three=[3]');
echo
'mergeQuery(\'one=[1]&two=[2]&three=[3]\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->mergeFilePath('./path/to/yet/another/php_script.php');
echo
'mergeFilePath(\'./path/to/yet/another/php_script.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->mergeFilePathQuery('../?six=[6]&seven=[7]');
echo
'mergeFilePathQuery(\'../?six=[6]&seven=[7]\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->mergeSchemeHost('http://local-host:4244/no/path/at/all.php');
echo
'mergeSchemeHost(\'http://local-host:4244/no/path/at/all.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->mergeSchemeHostPath('http://local_host:4243/no/path/at/all.php');
echo
'mergeSchemeHostPath(\'http://local_host:4243/no/path/at/all.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

$link->mergeSchemeHostFilePath('http://localhost:4242/no/path/at/all.php');
echo
'mergeSchemeHostFilePath(\'http://localhost:4242/no/path/at/all.php\')' . PHP_EOL . PHP_EOL . output($link) . PHP_EOL . PHP_EOL . '<hr>' . PHP_EOL;

highlight_file('../library/URLUtility.php');

/**
 * @param URLUtility $link
 * @return string
 */
function output($link)
{
    return
       
'getURL() : ' . $link->getURL() . PHP_EOL .
       
'getSchemeHost() : ' . $link->getSchemeHost() . PHP_EOL .
       
'getSchemeHostPath() : ' . $link->getSchemeHostPath() . PHP_EOL .
       
'getSchemeHostFilePath() : ' . $link->getSchemeHostFilePath() . PHP_EOL .
       
'getScheme() : ' . $link->getScheme() . PHP_EOL .
       
'getHost() : ' . $link->getHost() . PHP_EOL .
       
'getPort() : ' . $link->getPort() . PHP_EOL .
       
'getPath() : ' . $link->getPath() . PHP_EOL .
       
'getFileName() : ' . $link->getFileName() . PHP_EOL .
       
'getFilePath() : ' . $link->getFilePath() . PHP_EOL .
       
'getQuery() : ' . $link->getQuery() . PHP_EOL .
       
'getFilePathQuery() : ' . $link->getFilePathQuery();
}

?>
</pre>
</body>
</html>


  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file example.URLUtility.php Example URL Utility example
Plain text file URLUtility.php Class URL Utility class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:584
This week:0
All time:5,298
This week:140Up
 User Ratings  
 
 All time
Utility:68%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:-
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:49%StarStarStar
Rank:2849