aboutsummaryrefslogtreecommitdiffhomepage
path: root/misc/hostname_from_uri.php
blob: 0ee6b05823399dcde7f10875a027b1df1d0e77f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

/**
 * Extracts the hostname (and port) from the given URI, assuming it is of a known scheme and can have one
 * @param string $uri
 * @return ?string the host:port pair if there is one or null otherwise
 */
function hostname_from_uri(string $uri): ?string {
    if (!(
        str_starts_with($uri, 'https://') ||
        str_starts_with($uri, 'http://') ||
        str_starts_with($uri, 'gopher://') ||
        str_starts_with($uri, 'gemini://') ||
        str_starts_with($uri, 'mumble://')
    )) {
        return null;
    }
    return explode('/', $uri, 4)[2];
}