PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Murat Purc   mpUrlInfo   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Several examples of usage
Class: mpUrlInfo
Parse and compose URL from its parts
Author: By
Last change:
Date: 14 years ago
Size: 10,481 bytes
 

Contents

Class file image Download
<?php /** * Several examples for usage of mpUrlInfo. * * @package mpUrlInfo * @author Murat Purc <murat@purc.de> * @copyright Copyright (c) 2008-2010 Murat Purc (http://www.purc.de) * @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2 * @version $Id: examples.php 2 2010-02-17 15:47:06Z purcaholic $ */ error_reporting(E_ALL); // first include mpUrlInfo require_once('mpUrlInfo.php'); ############################################################################### #### Create a URL UrlInfo instances from existing URLs // URL with hostname $url = 'http://hostname.tld/'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; // URL with hostname and path $url = 'http://hostname.tld/path/to/page/'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; // URL with hostname, path and query parameter $url = 'http://hostname.tld/path/to/page/?foo=bar&amp;num=123'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; // URL with hostname, path, query parameter and fragment $url = 'http://hostname.tld/path/to/page/?foo=bar&amp;num=123#content_xy'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; ############################################################################### #### Build URL UrlInfo from scratch $urlInfo = new mpUrlInfo(); $urlInfo->setScheme('http'); $urlInfo->setHost('hostname.tld'); $urlInfo->setPath('/path/to/page.html'); $urlInfo->setQueryParameter('foo', 'bar'); $urlInfo->setQueryParameter('num', '123'); $urlInfo->setFragment('content_xy'); echo (string) $urlInfo . "\n"; ############################################################################### #### Examples for usage of equals() method. // sample URL $url = 'http://hostname.tld/path?foo=bar&num=123'; $urlInfo = new mpUrlInfo($url); // check, if $urlInfo equals itself if ($urlInfo->equals($urlInfo)) { echo (string) $urlInfo . " equals " . (string) $urlInfo . "\n"; } else { echo (string) $urlInfo . " don't equals " . (string) $urlInfo . "\n"; } // check, if $urlInfo equals a clone $urlInfo2 = clone $urlInfo; if ($urlInfo->equals($urlInfo2)) { echo (string) $urlInfo . " equals " . (string) $urlInfo2 . "\n"; } else { echo (string) $urlInfo . " don't equals " . (string) $urlInfo2 . "\n"; } // check, if $urlInfo equals a new instance $urlInfo2 = new mpUrlInfo($url); if ($urlInfo->equals($urlInfo2)) { echo (string) $urlInfo . " equals " . (string) $urlInfo2 . "\n"; } else { echo (string) $urlInfo . " don't equals " . (string) $urlInfo2 . "\n"; } ############################################################################### #### Examples for usage of isSameUrl() method. // sample URL $url = 'http://hostname.tld/path?foo=bar&num=123'; $urlInfo = new mpUrlInfo($url); // check, if $urlInfo is the same as itself if ($urlInfo->isSameUrl($urlInfo)) { echo (string) $urlInfo . " is same as " . (string) $urlInfo . "\n"; } else { echo (string) $urlInfo . " isn't same as " . (string) $urlInfo . "\n"; } // check, if $urlInfo is the same as new one which is identical except for additional fragment $url = 'http://hostname.tld/path?foo=bar&num=123#content_xy'; $urlInfo2 = new mpUrlInfo($url); if ($urlInfo->isSameUrl($urlInfo2)) { echo (string) $urlInfo . " is same as " . (string) $urlInfo2 . "\n"; } else { echo (string) $urlInfo . " isn't same as " . (string) $urlInfo2 . "\n"; } // another example with different argument separator (uses '&amp;' instead of '&') $url = 'http://hostname.tld/path?foo=bar&amp;num=123#content_xy'; $urlInfo2 = new mpUrlInfo($url); if ($urlInfo->isSameUrl($urlInfo2)) { echo (string) $urlInfo . " is same as " . (string) $urlInfo2 . "\n"; } else { echo (string) $urlInfo . " isn't same as " . (string) $urlInfo2 . "\n"; } ############################################################################### #### Examples for usage of arguments separator methods. // NOTE: A defined arguments separator takes allways precedence over existing // arguments separator in source URLs // save current arguments separator, it's '&amp;' by default $argSeparator = mpUrlInfo::getArgSeparator(); // set new arguments separator mpUrlInfo::setArgSeparator('&'); // sample url $url = 'http://hostname.tld/path?foo=bar&num=123'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; // another sample url $url = '/path?foo=bar&num=123&param3=valueX'; $urlInfo = new mpUrlInfo($url); echo (string) $urlInfo . "\n"; // restore original arguments separator mpUrlInfo::setArgSeparator($argSeparator); ############################################################################### #### Example for setUrl() method. $urlInfo = new mpUrlInfo(); $url = 'http://hostname.tld/path?foo=bar&amp;num[]=123&amp;num[]=124&amp;num[]=125'; $urlInfo->setUrl($url); echo (string) $urlInfo . "\n"; ############################################################################### #### Examples for trimPath() method. // NOTE: Trim path returns the path cleaned from beginning or ending slashes '/' $path = mpUrlInfo::trimPath('/path/to/page/'); echo $path . "\n"; $path = mpUrlInfo::trimPath('path/to/page/'); echo $path . "\n"; $path = mpUrlInfo::trimPath('path/to/page'); echo $path . "\n"; ############################################################################### #### Examples for setQueryParameter() method. $url = 'http://hostname.tld/path'; $urlInfo = new mpUrlInfo($url); // several query parameter $urlInfo->setQueryParameter('foo', 'bar'); $urlInfo->setQueryParameter('num', '123'); $query = $urlInfo->getQuery(); echo $query . "\n"; // query parameter as array $params = array('valueOf_0', 'valueOf_1', 'valueOf_2'); $urlInfo->setQueryParameter('multiple', $params); $query = $urlInfo->getQuery(); echo $query . "\n"; ############################################################################### #### Examples for setScheme() method. // NOTE: Only schemes 'http', 'https' and 'ftp' are supported $urlInfo = new mpUrlInfo(); $urlInfo->setScheme('http'); echo $urlInfo->getScheme() . "\n"; $urlInfo->setScheme('HTTP'); echo $urlInfo->getScheme() . "\n"; $urlInfo->setScheme('https'); echo $urlInfo->getScheme() . "\n"; $urlInfo->setScheme('ftp'); echo $urlInfo->getScheme() . "\n"; // set empty scheme to cause an error try { $urlInfo->setScheme(''); } catch (Exception $e) { // exception handling echo $e->getMessage() . "\n"; } // set a not supported scheme to cause an error try { $urlInfo->setScheme('asdf'); } catch (Exception $e) { // exception handling echo $e->getMessage() . "\n"; } ############################################################################### #### Examples for setHost() method. $urlInfo = new mpUrlInfo(); $urlInfo->setHost('foobar'); echo $urlInfo->getHost() . "\n"; // set a empty host to cause an error try { $urlInfo->setHost(''); } catch (Exception $e) { // exception handling echo $e->getMessage() . "\n"; } ############################################################################### #### Examples for setPort() method. $urlInfo = new mpUrlInfo(); $urlInfo->setPort('12345'); echo $urlInfo->getPort() . "\n"; $urlInfo->setPort(80); echo $urlInfo->getPort() . "\n"; // set a empty port to cause an error try { $urlInfo->setPort(''); } catch (Exception $e) { // exception handling echo $e->getMessage() . "\n"; } // set a invalid port to cause an error try { $urlInfo->setPort(999999); } catch (Exception $e) { // exception handling echo $e->getMessage() . "\n"; } ############################################################################### #### Examples for setUser() method. $urlInfo = new mpUrlInfo(); $urlInfo->setUser('John Doe'); echo $urlInfo->getUser() . "\n"; // encoded version of user $urlInfo->setUser('John+Doe'); echo $urlInfo->getUser() . "\n"; ############################################################################### #### Examples for setPass() method. $urlInfo = new mpUrlInfo(); $urlInfo->setPass('open sesame'); echo $urlInfo->getPass() . "\n"; // encoded version of password $urlInfo->setPass('open+sesame'); echo $urlInfo->getPass() . "\n"; ############################################################################### #### Examples for setPath() method. $urlInfo = new mpUrlInfo(); $urlInfo->setPath('path/to/page'); echo $urlInfo->getPath() . "\n"; $urlInfo->setPath('path to page'); echo $urlInfo->getPath() . "\n"; // encoded version of path $urlInfo->setPath('path+to+page'); echo $urlInfo->getPath() . "\n"; // setting empty path returns allways a slash! $urlInfo->setPath(''); echo $urlInfo->getPath() . "\n"; ############################################################################### #### Examples for setFilename() method. $urlInfo = new mpUrlInfo(); // filename without a extension $urlInfo->setFilename('foobar'); echo $urlInfo->getFilename() . "\n"; // filename without html extension $urlInfo->setFilename('page.html'); echo $urlInfo->getFilename() . "\n"; // no filename of a path with ending slash $urlInfo->setPath('/path/to/page/'); echo $urlInfo->getFilename() . "\n"; // path without ending slash returns the last item as filename $urlInfo->setPath('/path/to/page'); echo $urlInfo->getFilename() . "\n"; // should echo 'page' ############################################################################### #### Examples for setExtension() method. // url with ending slash in path... $url = 'http://hostname.tld/path/to/page/'; $urlInfo = new mpUrlInfo($url); // ... can't have a extension! $urlInfo->setExtension('html'); echo $urlInfo->getExtension() . "\n"; // second try should work $urlInfo->setPath('/path/to/page'); $urlInfo->setExtension('html'); echo $urlInfo->getExtension() . "\n"; echo $urlInfo->getFilename() . "\n"; // change extension $urlInfo->setExtension('php'); echo $urlInfo->getExtension() . "\n"; echo $urlInfo->getFilename() . "\n"; // set empty extzension $urlInfo->setExtension(''); echo $urlInfo->getExtension() . "\n"; echo $urlInfo->getFilename() . "\n";