Home » Programming » PHP » Php Parse Url

How to use the PHP parse_url() Function, With Examples

The PHP parse_url() function processes a given URL and divides it into its individual components.  Here’s how to use it.

parse_url() is often used to get the host/domain name from a given URL or the path to a remote host file.

Syntax for parse_url()

parse_url ( $url , $component )

Note that:

  • $url is the URL (e.g., https://www.linuxscrew.com) which you want to parse
    • Relative URLs may not be parsed correctly
  • $component is optional and can be one of:
    • PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT
    • …if you wish to retrieve only one component from the parsed URL
  • parse_url() returns one of three values
    • FALSE if it is passed a seriously malformed or mistyped URL
    • An associative array with values from the below components table
    • Or, if the $component is specified, the value of that component (or null if it is not present) – will be a String variable unless you are requesting PHP_URL_PORT, which will be an integer
URL components returned by parse_url()
scheme The URL scheme, for example: HTTP, HTTPS, FTP, file
host The hostname component of the URL
port The TCP/IP port component of the URL
user The user/username component of the URL
pass The password component of the URL
path The path component of the URL – everything after the first forward slash /
query The query string component – everything after the question mark?
fragment The fragment component of the URL – everything after the hash #

Note that parse_url() is not intended to handle URIs – just URLs. The terms are often used interchangeably, but they are not the same thing.

Examples

Here’s a URL that contains all of the components listed above and the result of running parse_url() on it:

$url = 'https://username:password@hostname:8080/path/to/file?argument=value#anchor';

var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_USER));
var_dump(parse_url($url, PHP_URL_PASS));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PORT));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url($url, PHP_URL_FRAGMENT));

The PHP var_dump() function is used to output the returned array’s details, followed by each component parsed individually.

The above code will output:

array(8) {
    ["scheme"]=>
    string(4) "https"
    ["host"]=>
    string(8) "hostname"
    ["port"]=>
    int(8080)
    ["user"]=>
    string(8) "username"
    ["pass"]=>
    string(8) "password"
    ["path"]=>
    string(5) "/path/to/file"
    ["query"]=>
    string(9) "argument=value"
    ["fragment"]=>
    string(6) "anchor"
}
string(4) "https"
string(8) "username"
string(8) "password"
string(8) "hostname"
int(8080)
string(5) "/path/to/file"
string(9) "argument=value"
string(6) "anchor"


For more parse_url examples, check out the official PHP documentation.

For more PHP explainers, check out our other PHP articles.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment