(PHP 4, PHP 5, PHP 7)
parse_url — PHP parsing URL and return its components
PHP parsing URL Description
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
This function is not meant to validate the given PHP parsing URL, it only breaks it up into the above listed parts. Partial URLs are also accepted,
parse_url() tries its best to parse them correctly.
The PHP parsing URL. Invalid characters are replaced by _.
Specify one of
PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when
PHP_URL_PORT is given, in which case the return value will be an integer).
On seriously malformed URLs,
parse_url() may return
component parameter is omitted, an associative array is returned. PHP parsing URL. At least one element will be present within the array. Potential keys within this array are:
- scheme – e.g. HTTP
- query – after the question mark?
- fragment – after the hashmark #
component parameter is specified,
parse_url() returns a string (or an integer, in the case of
PHP_URL_PORT) instead of an array. If the requested component doesn’t exist within the given URL,
NULL will be returned.
|5.4.7||Fixed host recognition when scheme is omitted and a leading component separator is present.|
|5.3.3||Removed the |
|5.1.2||Added the |
A parse_url() example
The above example will output:
A parse_url() example with missing scheme
The above example will output:
This function doesn’t work with relative URLs.
This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP’s backward compatibility requirements it makes an exception for the file:// scheme where triple slashes (file:///…) are allowed. For any other scheme, this is invalid.
If you haven’t yet been able to find a simple conversion back to string from a parsed url, here’s an example:
Here is utf-8 compatible parse_url() replacement function based on “laszlo dot janszky at gmail dot com” work. PHP parsing URL. Original incorrectly handled URLs with user:pass. Also made PHP 5.5 compatible (got rid of now deprecated regex /e modifier).
Created another parse_url utf-8 compatible function.
Here’s a good way to using parse_url () gets the youtube link.
This function I used in many works:
parse_url () allocates a unique youtube code and put into iframe link and displayed on your page. PHP parsing URL. The size of the videos choose yourself.
Based on the idea of “jbr at ya-right dot com” have I been working on a new function to parse the url:
So both named and numbered array keys are possible.
Some example that determines the URL port.
When port not specified, it derives it from the scheme.
Here’s a simple class I made that makes use of this parse_url.
I needed a way for a page to retain get parameters but also edit or add onto them. PHP parsing URL.
I also had some pages that needed the same GET parameters so I also added a way to change the path.
Hope this is of some use to someone!
UTF-8 aware parse_url() replacement.
I’ve realized that even though UTF-8 characters are not allowed in URL’s, I have to work with a lot of them and parse_url() will break.
Based largely on the work of “mallluhuct at gmail dot com”, I added parse_url() compatible “named values” which makes the array values a lot easier to work with (instead of just numbers). I also implemented detection of port, username/password and a back-reference to better detect URL’s like this: //en.wikipedia.com
… which, although is technically an invalid URL, it’s used extensively on sites like wikipedia in the href of anchor tags where it’s valid in browsers (one of the types of URL’s you have to support when crawling pages). This will be accurately detected as the host name instead of “path” as in all other examples.
I will submit my complete function (instead of just the RegExp) which is an almost “drop-in” replacement for parse_url(). It returns a cleaned up array (or false) with values compatible with parse_url(). I could have told the preg_match() not to store the unused extra values, but it would complicate the RegExp and make it more difficult to read, understand and extend. The key to detecting UTF-8 characters is the use of the “u” parameter in preg_match().
UTF-8 URL’s can/should be “normalized” after extraction with this function.