摘要:從網址中取得網域名稱
這也是在ASPFAQ看到的...
一樣是用Split()的函數來達成...
Function ParseDomainFromURL(url)
urlParts = split(url,"/")
ParseDomainFromURL = urlParts(2)
End Function
complexURL = "http://foo.com/whatever.asp?foo=1&bar=2"
domain = parseDomainFromURL(complexURL)
response.write "Domain = <b>" & domain & "</b>"
urlParts = split(url,"/")
ParseDomainFromURL = urlParts(2)
End Function
complexURL = "http://foo.com/whatever.asp?foo=1&bar=2"
domain = parseDomainFromURL(complexURL)
response.write "Domain = <b>" & domain & "</b>"
但上上面這函數有點問題,就是如果他的網址不包含(//) 的話,取得的資料就有問題了...
(ps.因為網址通常是有http://或是https://開頭.)
所以需要在函數中加些判斷,如下:
Function ParseDomainFromURL(url)
if instr(url, "//") > 0 then
urlParts = split(url,"/")
ParseDomainFromURL = urlParts(2)
else
ParseDomainFromURL = "Invalid URL"
end if
End Function
if instr(url, "//") > 0 then
urlParts = split(url,"/")
ParseDomainFromURL = urlParts(2)
else
ParseDomainFromURL = "Invalid URL"
end if
End Function