Methods

Base constructor, accepts a url or by default uses the current full URL of the page being visited.

__construct(string $url) : object

Parameters

$url

string

Returns

object

Appends a $key => $value array into the URL's query string.

addQuery(array $query) : object

eg...

$url = new URLParser\URL('http://www.example.com'); $url->addQuery(array( 'val1' => 'stuff', 'val2' => 'more stuff', )); echo $url->make();

Outputs...

    http://www.example.com?val1=stuff&val2=more+stuff

Parameters

$query

array

Returns

object

Sets or returns the anchor tag at the end of the URL

anchor(string $anchor) : mixed

eg...

$url = new URLParser\URL('http://www.example.com'); $url->anchor('products'); echo $url->make();

Outputs...

    http://www.example.com/#products

Parameters

$anchor

string

Returns

mixed

Appends a specified text segment to the URL path.

appendSegment(string $newSegment, string $appendAfter) : object

Optionally a segment can be specified ($appendAfter) and the new segment will be inserted immediately following the instance of $appendAfter.

eg...

$url = new URLParser\URL('http://www.example.com/products/services'); $url->appendSegment('test'); echo $url->make() . "<br/>"; $url->appendSegment('argh','products'); echo $url->make() . "<br/>";

Outputs...

    http://www.example.com/products/services/test
    http://www.example.com/products/argh/services/test

Parameters

$newSegment

string

$appendAfter

string

Returns

object

Sets or returns the host (domain) for the url.

host(string $host) : mixed

eg...

$url = new URLParser\URL('http://www.example.com/stuff?val1=foo'); $url->host('www.google.ca'); echo $url->make();

Outputs...

    http://www.google.ca/stuff?val1=foo

Parameters

$host

string

Returns

mixed

make()

make() 

Sets or returns the password for auth.

pass(string $pass) : mixed

User must be specified first.

eg...

$url = new URLParser\URL('http://www.example.com/stuff?val1=foo'); $url->user('joe'); $url->pass('foobar'); echo $url->make();

Outputs...

    http://joe:foobar@www.example.com/stuff?val1=foo

Parameters

$pass

string

Returns

mixed

Sets or returns the port for the url.

port(string $port) : mixed

eg...

$url = new URLParser\URL('http://www.example.com/stuff?val1=foo'); $url->port(8080); echo $url->make();

Outputs...

    http://www.example.com:8080/stuff?val1=foo

Parameters

$port

string

Returns

mixed

Prepends a specified text segment to the URL path.

prependSegment(string $newSegment, string $prependBefore) : object

Optionally a segment can be specified ($prependBefore) and the new segment will be inserted immediately before the instance of $prependBefore.

eg...

$url = new URLParser\URL('http://www.example.com/products/services'); $url->prependSegment('test'); echo $url->make() . "<br/>"; $url->prependSegment('argh','products'); echo $url->make() . "<br/>";

Outputs...

    http://www.example.com/test/products/services
    http://www.example.com/test/argh/products/services

Parameters

$newSegment

string

$prependBefore

string

Returns

object

Sets or returns the protocol for the URL (http/ftp/etc.).

protocol(string $protocol) : mixed

eg...

$url = new URLParser\URL('http://www.example.com'); $url->protocol('ftp'); echo $url->make();

Outputs...

    ftp://www.example.com

Parameters

$protocol

string

Returns

mixed

Returns the query string for the current URL or, if $query is specified returns the value for that parameter

query(string $query) : string

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); echo $url->query('val1') . "<br/>"; echo $url->query('val2') . "<br/>"; echo $url->query('val3') . "<br/>"; // Returns FALSE echo $url->query() . "<br/>";

Outputs...

    products
    services

    val1=products&val2=services

Parameters

$query

string

Returns

string

Returns the query string for the current URL as a $key => $value array.

queryArray() : array

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); var_dump($url->queryArray());

Outputs...

array(2) { ["val1"]=> string(8) "products" ["val2"]=> string(8) "services" }

Returns

array

Returns the segments (path) string for the current URL as an array.

segmentArray() : array

eg...

$url = new URLParser\URL('http://www.example.com/products/services'); var_dump($url->segmentArray());

Outputs...

array(2) { [0]=> string(8) "products" [1]=> string(8) "services" }

Returns

array

Returns the the entire segment path if $search is false or looks for $search in the URL path and returns true / false.

segments(string $search) : mixed

eg...

$url = new URLParser\URL('http://www.example.com/products/services'); echo $url->segments('products') . "<br/>"; echo $url->segments('services') . "<br/>"; echo $url->segments('abcd') . "<br/>"; // Returns FALSE echo $url->segments() . "<br/>";

Outputs...

    (true)
    (true)
    (false)
    /products/services

Parameters

$search

string

Returns

mixed

Strips the ENTIRE query string from the url.

stripQueries() : object

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); $url->stripQueries(); echo $url->make();

Outputs...

    http://www.example.com

Returns

object

Strips a query called "$query" from the query string.

stripQuery(string $query) : object

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); $url->stripQuery('val1'); echo $url->make();

Outputs...

    http://www.example.com?val2=services

Parameters

$query

string

Returns

object

Strips a segment called $s from the URI.

stripSegment(string $s) : object

eg...

$url = new URLParser\URL('http://www.example.com/products/services'); $url->stripSegment('products'); echo $url->make();

Outputs...

    http://www.example.com/services

Parameters

$s

string

Returns

object

Strips all segments from the URL

stripSegments() : object

eg...

$url = new URLParser\URL('http://www.example.com/products/services?val1=stuff&val2=more+stuff'); $url->stripSegments(); echo $url->make();

Outputs...

    http://www.example.com?val1=stuff&val2=more+stuff

Returns

object

Renames a set of query parameters from a key => value array of oldname => newname

swapQueries($queries) : object

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); $url->swapQueries(array( 'val1' => 'valX', 'val2' => 'valY', )); echo $url->make();

Outputs...

    http://www.example.com?valX=products&valY=services

Parameters

$queries

Returns

object

Renames a query parameter from $oldName to $newName

swapQuery($oldName, $newName) : object

eg...

$url = new URLParser\URL('http://www.example.com?val1=products&val2=services'); $url->swapQuery('val1','valX'); echo $url->make();

Outputs...

    http://www.example.com?val2=services&valX=products

Parameters

$oldName

$newName

Returns

object

Swaps segment names using $key => $value input where $oldname => $newname.

swapSegments(array $newSegments) : object

eg...

$url = new URLParser\URL('http://www.example.com/oldSegment/home'); $url->swapSegments(array( 'oldSegment' => 'newSegment', 'home' => 'main', )); echo $url->make();

Outputs...

    http://www.example.com/newSegment/main

Parameters

$newSegments

array

Returns

object

Swaps the base URL the parser is operating on for the URL passed in $url.

to(string $url) : object

This is useful for scenarios where you wish to batch add query strings to multiple URLs. You can setup the queries on one object and then to() and make() multiple calls to generate new URLs.

Parameters

$url

string

Returns

object

Sets or returns the user for auth.

user(string $user) : mixed

eg...

$url = new URLParser\URL('http://www.example.com/stuff?val1=foo'); $url->user('joe'); echo $url->make();

Outputs...

    http://joe@www.example.com/stuff?val1=foo

Parameters

$user

string

Returns

mixed

Parses and returns the full URL of the page currently being visited.

currentURL() : string

Used by the __construct method.

Returns

string

insertIntoArrayAtKey()

insertIntoArrayAtKey($array, $key, $newKeyValuePairs, $offset) 

Parameters

$array

$key

$newKeyValuePairs

$offset

Lookups up $oldKey in $array and renames the key to $newKey

renameArrayKey(array $array, string $oldKey, string $newKey) : array

eg...

$arr = array( 'foo' => 'bar', 'foo2' => 'bar2', );

$arr = $this->renameArrayKey( $arr, 'foo', 'test' );

returns...

array( 'test' => 'bar', 'foo2' => 'bar2', )

Parameters

$array

array

$oldKey

string

$newKey

string

Returns

array

Plural of renameArrayKey, allows passing of oldKey => newKey as an array.

renameArrayKeys(array $array, array $newKeys) : array

eg...

$arr = array( 'foo' => 'bar', 'foo2' => 'bar2', );

$arr = $this->renameArrayKey( $arr, array( 'foo' => 'test' ) );

returns...

array( 'test' => 'bar', 'foo2' => 'bar2', )

Parameters

$array

array

$newKeys

array

Returns

array

Takes an array and returns an array of 2 arrays.

sliceArrayAtKey(array $array, string $key, boolean $offset) : array
  • One array including everything BEFORE the specified key
  • Another array INCLUDING the specified key and everything AFTER

Optionally specify "$offset" to indicate that the slice should occur AFTER $offset keys, meaning the function will return...

  • One array including everything up to and INCLUDING the specified key plus $offset keys later
  • Another array including everything AFTER the specified key plus $offset keys

In the case that $key does not exist or is not specified the function will still return 2 arrays. If $offset is not specified or is less than 0 (prepend mode), the returned array at index 0 will contain a blank array and index 1 will contain all of the elements from the original array. If $offset is specified and is greater than 0 (append mode) index 0 will contain all of the elements from the original array and index 1 will contain a blank array.


Example 1:

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

Note: $offset not specificed (0) means the function operates in "prepend" mode

$arr = $this->sliceArrayAtKey( $arr, 'key2' );

returns....

array( [0] => array( 'key1' => 'val1', ), [1] => array( 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', ), )


Example 2:

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

Note: $offset > 0 (1) means the function operates in "append" mode

$arr = $this->sliceArrayAtKey( $arr, 'key2', 1 );

returns....

array( [0] => array( 'key1' => 'val1', 'key2' => 'val2', ), [1] => array( 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', ), )


Example 3:

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

$arr = $this->sliceArrayAtKey( $arr, 'key2', 3 );

returns....

array( [0] => array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', ), [1] => array( 'key5' => 'val5', ), )


Example 4:

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

$arr = $this->sliceArrayAtKey( $arr, 'key2', -1 );

returns....

array( [0] => array( ), [1] 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key3' => 'val4', 'key5' => 'val5', ), )


Example 5:

Note: $offset is not specified and the function defaults to "prepend" mode, $key does not exist.

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

$arr = $this->sliceArrayAtKey( $arr, 'key6' );

returns.... array( [0] => array( ), [1] 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key3' => 'val4', 'key5' => 'val5', ), )


Example 6:

Note: $offset is 1 and the function defaults to "append" mode, $key does not exist

$arr = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key4' => 'val4', 'key5' => 'val5', );

$arr = $this->sliceArrayAtKey( $arr, 'key6', 1 );

returns.... array( [0] => array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', 'key3' => 'val4', 'key5' => 'val5', ), [1] ), )

Parameters

$array

array

$key

string

$offset

boolean

Returns

array

 Properties

 

$resetURL : array
 

$url : array

This is the property which is actively used and modified throughout the functions of this class.