GetHrefs Function:
GetHrefs Function parses a string for html anchor tags and returns them.
Description:
GetHrefs searches a string representing a html page for html anchor tags and returns any links found in the html. GetHrefs returns a scripting.dictionary containing 0 or more pairs or key/value combinations. The key is the url as written within the HREF="" part of an anchor tag and the value is whatever is between the tags as the name of the link. You can html encode the name of the link as it could contain html. GetHrefs Function needs the GetWebPage Function to work.
Syntax:
Set dictionary = GetHrefs( html )
Details:
The following examples illustrate how GetHrefs parses anchor tags and what it returns:
html: <A HREF="./page1.html"><IMG SRC="img1.gif" BORDER=0></A>
dictionary pair key = "./page1.html" value = "<IMG SRC="img1.gif" BORDER=0>"
html: <A HREF="http://site.com">my site</A>
dictionary pair key = "http://site.com"; value = "my site"
html: <A HREF="../../folder/file.asp?folder=1&file=2">my favorite page</A>
dictionary pair key = "../../folder/file.asp?folder=1&file=2" value = "my favorite page"
Example:
<%
dim href, d, htmltxt
'--- Use the <a href="../main/asp_functions.asp?id=GetWebPage Function">GetWebPage function</a> htmltxt = GetWebPage("http://www.livio.net/main/default.asp", "")
'--- call GetHrefs function to parse html text Set d = GetHrefs(htmltxt)
'--- loop through returned dictionary of urls and anchor names For Each href in d.keys Response.Write("key = " & href & "<BR>") Response.Write("value = " & Server.HtmlEncode(d(href)) & "<BR>") Next
Set d = Nothing
%>
ASP Source Code:
<%
Private Function GetHrefs(ByVal html) dim re, matches, match, d, uri, name
Set d = createobject("scripting.dictionary") Set re = new regexp re.pattern = "<A(.+?)HREF=""(.+?)""(.+?)>(.+?)</A>" re.ignorecase = true re.multiline = true re.global = true Set matches = re.execute(html) For Each match in matches uri = match.submatches(1) name = match.submatches(3)
if not d.exists(uri) then d.add uri, name Next Set matches = Nothing Set re = Nothing
Set gethrefs = d End Function
%>
|