NET_HOST函数用于从STRING类型的URL字符串中解析主机名(hostname)部分。
命令格式
STRING NET_HOST(STRING <url>)
参数说明
url:必填。STRING类型。待解析的URL字符串。
说明
为了获得最佳解析结果,URL字符串应符合RFC 3986定义的格式。
返回值说明
返回STRING类型。解析给定的URL并返回其中的主机名部分。
若包含端口号,端口号也会保留在返回结果中。
若函数无法解析输入的URL,则返回NULL。
相关解析函数
若需要解析URL中除主机名的其他部分,可以使用如下函数:
NET_PUBLIC_SUFFIX:解析给定的URL中的公共后缀(如com、org或net)。
NET_REG_DOMAIN: 解析给定的URL中已注册或可注册的域名(即公共后缀加上前一级标签)。
使用示例
SELECT input
,description
,NET_HOST(input) AS HOST
,NET_PUBLIC_SUFFIX(input) AS SUFFIX
,NET_REG_DOMAIN(input) AS DOMAIN
FROM (
SELECT "" AS input, "invalid input" AS description
UNION ALL SELECT "http://abc.xyz", "standard URL"
UNION ALL SELECT "//user:password@a.b:80/path?query",
"standard URL with relative scheme, port, path and query, but no public suffix"
UNION ALL SELECT "https://[::1]:80", "standard URL with IPv6 host"
UNION ALL SELECT "http://例子.卷筒纸.中国", "standard URL with internationalized domain name"
UNION ALL SELECT " www.Example.Co.UK ",
"non-standard URL with spaces, upper case letters, and without scheme"
UNION ALL SELECT "mailto:?to=&subject=&body=", "URI rather than URL--unsupported"
);
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+
| input | description | host | suffix | domain |
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+
| | invalid input | NULL | NULL | NULL |
| http://abc.xyz | standard URL | abc.xyz | xyz | abc.xyz |
| //user:password@a.b:80/path?query | standard URL with relative scheme, port, path and query, but no public suffix | a.b | NULL | NULL |
| https://[::1]:80 | standard URL with IPv6 host | [::1] | NULL | NULL |
| http://例子.卷筒纸.中国 | standard URL with internationalized domain name | 例子.卷筒纸.中国 | 中国 | 卷筒纸.中国 |
| www.Example.Co.UK | non-standard URL with spaces, upper case letters, and without scheme | www.Example.Co.UK | Co.UK | Example.Co.UK |
| mailto:?to=&subject=&body= | URI rather than URL--unsupported | mailto | NULL | NULL |
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+
相关函数
NET_HOST函数属于网络函数,更多网络相关函数请参见网络函数。
该文章对您有帮助吗?