Properties

$url

$url : array

The actively used URL in the format dictated by PHP's built-in parse_url function. This is the property which is actively used and modified throughout the functions of this class.

Type

array

$resetURL

$resetURL : array

Same as $url but always contains a blank array, used by the _to() method to reset the current URL property when overwriting with a new base URL.

Type

array

Methods

__construct()

__construct(string $url) : object

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

Parameters

string $url

Returns

object

to()

to(string $url) : object

Swaps the base URL the parser is operating on for the URL passed in $url. 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

string $url

Returns

object

queryArray()

queryArray() : array

Returns the query string for the current URL as a $key => $value 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

query()

query(string $query) : string

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

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

string $query

Returns

string

addQuery()

addQuery(array $query) : object

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

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

array $query

Returns

object

stripQuery()

stripQuery(string $query) : object

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

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

string $query

Returns

object

stripQueries()

stripQueries() : object

Strips the ENTIRE query string from the url.

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

swapQuery()

swapQuery( $oldName,  $newName) : object

Renames a query parameter from $oldName to $newName

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

swapQueries()

swapQueries( $queries) : object

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

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

segmentArray()

segmentArray() : array

Returns the segments (path) string for the current URL as an 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

segments()

segments(string $search) : mixed

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

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

string $search

Returns

mixed

appendSegment()

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

Appends a specified text segment to the URL path. 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

string $newSegment
string $appendAfter

Returns

object

prependSegment()

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

Prepends a specified text segment to the URL path. 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

string $newSegment
string $prependBefore

Returns

object

stripSegment()

stripSegment(string $s) : object

Strips a segment called $s from the URI.

eg...

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

Outputs...

    http://www.example.com/services

Parameters

string $s

Returns

object

stripSegments()

stripSegments() : object

Strips all segments from the URL

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

swapSegments()

swapSegments(array $newSegments) : object

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

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

array $newSegments

Returns

object

anchor()

anchor(string $anchor) : mixed

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

eg...

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

Outputs...

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

Parameters

string $anchor

Returns

mixed

protocol()

protocol(string $protocol) : mixed

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

eg...

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

Outputs...

    ftp://www.example.com

Parameters

string $protocol

Returns

mixed

host()

host(string $host) : mixed

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

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

string $host

Returns

mixed

port()

port(string $port) : mixed

Sets or returns the port for the url.

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

string $port

Returns

mixed

user()

user(string $user) : mixed

Sets or returns the user for auth.

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

string $user

Returns

mixed

pass()

pass(string $pass) : mixed

Sets or returns the password for auth. 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

string $pass

Returns

mixed

make()

make()

currentURL()

currentURL() : string

Parses and returns the full URL of the page currently being visited. Used by the __construct method.

Returns

string

renameArrayKey()

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

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

eg...

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

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

returns...

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

Parameters

array $array
string $oldKey
string $newKey

Returns

array

renameArrayKeys()

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

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

eg...

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

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

returns...

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

Parameters

array $array
array $newKeys

Returns

array

sliceArrayAtKey()

sliceArrayAtKey(array $array, string $key, boolean $offset) : array

Takes an array and returns an array of 2 arrays.

  • 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
string $key
boolean $offset

Returns

array

insertIntoArrayAtKey()

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

Parameters

$array
$key
$newKeyValuePairs
$offset