$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.
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.
| string | $url |
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" }
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
| string | $query |
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
| array | $query |
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
| string | $query |
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
| $oldName | ||
| $newName |
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
| $queries |
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" }
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
| string | $search |
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
| string | $newSegment | |
| string | $appendAfter |
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
| string | $newSegment | |
| string | $prependBefore |
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
| array | $newSegments |
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
| string | $pass |
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', )
| array | $array | |
| string | $oldKey | |
| string | $newKey |
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', )
| array | $array | |
| array | $newKeys |
sliceArrayAtKey(array $array, string $key, boolean $offset) : array
Takes an array and returns an array of 2 arrays.
Optionally specify "$offset" to indicate that the slice should occur AFTER $offset keys, meaning the function will return...
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] ), )
| array | $array | |
| string | $key | |
| boolean | $offset |