parse_url

parse_url(string $url, int $component = -1): int|string|array|null|false

Parses a URL and returns its components.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *url = "https://user:pass@example.com:8080/path/page?q=php#top";
    char scheme[16], host[64], port[8] = "", tail[128];

    /* C has no standard URL parser; hand-written split: %[^:] reads up to the colon for the scheme */
    if (sscanf(url, "%15[^:]://%63[^/]%127[^\n]", scheme, host, tail) != 3) {
        return 1;
    }
    /* the authority part may still carry userinfo@ and :port, split further */
    char *at = strrchr(host, '@');
    if (at) {
        memmove(host, at + 1, strlen(at + 1) + 1);
    }
    char *colon = strchr(host, ':');
    if (colon) {
        strcpy(port, colon + 1);
        *colon = '\0';
    }
    /* tail = path?query#fragment, cut off fragment, then query */
    char fragment[64] = "";
    char *hash = strchr(tail, '#');
    if (hash) {
        strcpy(fragment, hash + 1);
        *hash = '\0';
    }
    char query[64] = "";
    char *qmark = strchr(tail, '?');
    if (qmark) {
        strcpy(query, qmark + 1);
        *qmark = '\0';
    }

    printf("%s\n%s\n%s\n%s\n%s\n%s\n", scheme, host, port, tail, query, fragment);
    return 0;
}
Output
https
example.com
8080
/path/page
q=php
top

C++ C++26

C++26
#include <iostream>
#include <string>
int main() {
    std::string url = "https://user:pass@example.com:8080/path/page?q=php#top";

    // C++ stdlib has no URL parser either; hand-written splitting idiom
    size_t schemeEnd = url.find("://");
    std::string scheme = url.substr(0, schemeEnd);
    size_t authStart = schemeEnd + 3;
    size_t pathStart = url.find('/', authStart);
    std::string authority = url.substr(authStart, pathStart - authStart);
    size_t at = authority.rfind('@');
    if (at != std::string::npos) authority = authority.substr(at + 1); // drop userinfo
    size_t colon = authority.rfind(':');
    std::string host = authority.substr(0, colon);
    std::string port = authority.substr(colon + 1);

    std::string tail = url.substr(pathStart);
    size_t hash = tail.find('#');
    std::string fragment = hash == std::string::npos ? "" : tail.substr(hash + 1);
    if (hash != std::string::npos) tail = tail.substr(0, hash);
    size_t qmark = tail.find('?');
    std::string query = qmark == std::string::npos ? "" : tail.substr(qmark + 1);
    if (qmark != std::string::npos) tail = tail.substr(0, qmark);

    std::cout << scheme << "\n" << host << "\n" << port << "\n" << tail << "\n"
              << query << "\n" << fragment << "\n";
    return 0;
}
Output
https
example.com
8080
/path/page
q=php
top

C# v11.0.100

v11.0.100
var u = new Uri("https://user:pass@example.com:8080/path/page?q=php#top");
Console.WriteLine(u.Scheme);
Console.WriteLine(u.Host);
Console.WriteLine(u.Port);
Console.WriteLine(u.AbsolutePath);
Console.WriteLine(u.Query.TrimStart('?')); // Query carries a leading ?, strip it for the full query
Console.WriteLine(u.Fragment.TrimStart('#')); // Fragment carries a leading #
Output
https
example.com
8080
/path/page
q=php
top

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "net/url"
)

func main() {
    u, err := url.Parse("https://user:pass@example.com:8080/path/page?q=php#top")
    if err != nil {
        panic(err)
    }
    fmt.Println(u.Scheme)
    fmt.Println(u.Hostname()) // Hostname strips the port, Host keeps it
    fmt.Println(u.Port())
    fmt.Println(u.Path)
    fmt.Println(u.RawQuery)
    fmt.Println(u.Fragment)
}
Output
https
example.com
8080
/path/page
q=php
top

Java v27.0.1

v27.0.1
import java.net.URI;

public class Main {
    public static void main(String[] args) throws Exception {
        URI u = new URI("https://user:pass@example.com:8080/path/page?q=php#top");
        System.out.println(u.getScheme());
        System.out.println(u.getHost());
        System.out.println(u.getPort());
        System.out.println(u.getPath());
        System.out.println(u.getRawQuery());
        System.out.println(u.getFragment());
    }
}
Output
https
example.com
8080
/path/page
q=php
top

JavaScript v26.13.1

v26.13.1
const u = new URL("https://user:pass@example.com:8080/path/page?q=php#top");
console.log(u.protocol.slice(0, -1)); // protocol carries a trailing colon, strip it
console.log(u.hostname);
console.log(u.port);
console.log(u.pathname);
console.log(u.search.slice(1)); // search carries a leading ?, strip it for the full query
console.log(u.hash.slice(1)); // hash carries a leading #
Output
https
example.com
8080
/path/page
q=php
top

PHP v8.6.0

v8.6.0
$parts = parse_url("https://user:pass@example.com:8080/path/page?q=php#top");
echo $parts["scheme"], "\n", $parts["host"], "\n", $parts["port"], "\n",
    $parts["path"], "\n", $parts["query"], "\n", $parts["fragment"], "\n";
Output
https
example.com
8080
/path/page
q=php
top

Python v3.15.2

v3.15.2
from urllib.parse import urlparse

parts = urlparse("https://user:pass@example.com:8080/path/page?q=php#top")
print(parts.scheme)
print(parts.hostname)
print(parts.port)
print(parts.path)
print(parts.query)
print(parts.fragment)
Output
https
example.com
8080
/path/page
q=php
top

Rust v1.99.1

v1.99.1
fn main() {
    let url = "https://user:pass@example.com:8080/path/page?q=php#top";

    // std has no URL parsing library (ecosystem uses the third-party url crate); hand-written split idiom
    let (scheme, rest) = url.split_once("://").unwrap();
    let (authority, tail) = rest.split_once('/').unwrap();
    let authority = authority.rsplit_once('@').map(|(_, h)| h).unwrap_or(authority);
    let (host, port) = authority.split_once(':').unwrap();

    let (tail, fragment) = tail.split_once('#').unwrap_or((tail, ""));
    let (path, query) = tail.split_once('?').unwrap_or((tail, ""));

    println!("{scheme}\n{host}\n{port}\n/{path}\n{query}\n{fragment}");
}
Output
https
example.com
8080
/path/page
q=php
top

TypeScript v26.13.1

v26.13.1
const u: URL = new URL("https://user:pass@example.com:8080/path/page?q=php#top");
console.log(u.protocol.slice(0, -1));
console.log(u.hostname);
console.log(u.port);
console.log(u.pathname);
console.log(u.search.slice(1));
console.log(u.hash.slice(1));
Output
https
example.com
8080
/path/page
q=php
top

Language differences

C: no URL parser in the std — splits by hand with sscanf + strchr.

C++: no URL parser in the std — find/substr.

C#: Uri properties also carry leading characters (Query/Fragment), stripped in the examples to align with PHP; userinfo via UserInfo.

Go: net/url.Parse, object-style; userinfo via u.User.

Java: URI, object-style; userinfo via getUserInfo.

JavaScript: URL, object-style, but properties often stick to separators — protocol carries the trailing :, search the leading ?, hash the leading #; the examples strip them to align with PHP; userinfo via u.username.

PHP: component naming differs more than behavior — parse_url returns an associative array (scheme/host/port/path/query/fragment, keys only for parts present in the URL); the examples show only the six shared components; note it only splits strings and never decodes — to split the query into key/value pairs use parse_str (another entry in this batch).

Python: urllib.parse.urlparse returns a named tuple whose hostname/port are split out of netloc; userinfo via parts.username.

Rust: no URL parser in the std — split_once/rsplit_once (the ecosystem standard is the third-party url crate, not in this site's validator dependency whitelist, hence the hand-written idiom; prefer the crate in production).

TypeScript: same as JS — URL.