diff options
Diffstat (limited to 'misc/hostname_from_uri.php')
| -rw-r--r-- | misc/hostname_from_uri.php | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/misc/hostname_from_uri.php b/misc/hostname_from_uri.php new file mode 100644 index 0000000..0ee6b05 --- /dev/null +++ b/misc/hostname_from_uri.php @@ -0,0 +1,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]; +}
\ No newline at end of file |
