parse_url
Parses a URL and returns its components as an associative array or a specific component as a string.
Parameters
main.php
Return Values
mixed
Returns associative array of URL components or specific component value
On success:Array with URL parts (scheme, host, port, user, pass, path, query, fragment) or specific component
On failure:false for seriously malformed URLs, null for missing components
Notes
- Does not validate URLs - designed for parsing, not validation
- Use filter_var() with FILTER_VALIDATE_URL for URL validation
- Component constants: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, etc.
- May not handle relative URLs correctly - works best with absolute URLs
Changelog
- 4.3.0: Function was introduced
- 5.1.2: Added component parameter
- 5.4.7: Fixed parsing of scheme-relative URLs
Related Functions
explode
StringSplits a string by a delimiter and returns an array of strings.
splitparsecsv