URL Parse

url.parse() method Parse a URL and return its components

url.parse(string <url>) : mix

This function parses a URL and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded.

This function is not meant to validate the given URL, it only breaks it up into the parts listed below. Partial and invalid URLs are also accepted, url.parse() tries its best to parse them correctly.

Return Values

On seriously malformed URLs, url.parse() may return false.

Potential return array are:

  • scheme – e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query – after the question mark ?
  • fragment – after the hashmark #

If the requested component doesn’t exist within the given URL, null will be returned. url.parse() distinguishes absent and empty queries and fragments:

http://example.com/foo → query = null, fragment = null
http://example.com/foo? → query = "",   fragment = null
http://example.com/foo# → query = null, fragment = ""
http://example.com/foo?# → query = "",   fragment = ""

Example #1: URL String Decode

link = 'http://username:password@hostname:9090/path?arg=value#anchor';
url.parse(link).port ---> output: 9090
print(url.parse(link)); --> print all url information