View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Extract base domain from an URL?

Try some code like the following. It will parse URLs with the
following formats:

www.cpearson.com
www.cpearson.com/Page.aspx
www.cpearson.com/Page.aspx?param=1234
www.cpearson.com?param=1234
http://www.cpearson.com
http://www.cpearson.com/Page.aspx
http://www.cpearson.com/Page.aspx?param=1234
http://www.cpearson.com?param=1234

In all cases, it will retrun www.cpearson.com prefixed with "http://"
if that was present in the original URL.


Dim URL As String
Dim N As Long
Dim M As Long
Dim S As String

URL = "http://www.cpearson.com"
N = InStr(1, URL, "//") + 2
M = InStr(N, URL, "?")
N = InStr(N, URL, "/")
If N = 0 Then
If M = 0 Then
S = URL
Else
S = Left(URL, M - 1)
End If
Else
If M = 0 Then
S = Left(URL, N - 1)
Else
If M < N Then
S = Left(URL, M - 1)
Else
S = Left(URL, N - 1)
End If
End If
End If
Debug.Print S


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Sun, 31 May 2009 11:09:02 +0200, "Charlotte E" <@ wrote:


In Excel 2003 VBA, how to quickly extract a base domain from a given URL?

I.e.

http://www.domain.net/sub1/sub2/etc/page.html
would become
http://www.domain.net/

or...

ftp://ftp.server.net/sub1
would become
ftp://ftp.server.net/

or...

www.website.net
would become
http://www.website.net/

and also secure sites

https://www.securesite.net/index.asp
would become
https://www.securesite.net/

and maybe even

ftp.site.net
would become
ftp://ftp.site.net/



TIA,