
December 19th, 2006, 02:45 AM
|
 |
Moderator From Beyond
|
|
Join Date: Sep 2004
Location: Israel
|
|
Vbscript - automatic parsing of links inside text
use the code below to parse links that are inside text.
it contains example of usage as well, for any questions
or problems post here.
Code:
<% Option Explicit %>
<%
Function AddLinks(strText)
Dim result, arrWords, x
Dim curWord, blnLinkActive
result=""
arrWords=Split(strText, " ")
blnLinkActive = False
For x=0 TO UBound(arrWords)
curWord=arrWords(x)
If LCase(curWord)="<a" Then
blnLinkActive = True
End If
If LCase(curWord)="</a>" Then
blnLinkActive = False
End If
If Not(blnLinkActive) Then
If IsURL(curWord) Then
curWord="<a href=""" & MakeFullURL(curWord) & """>" & curWord & "</a>"
End If
End If
result = result & curWord
If x<UBound(arrWords) Then result=result&" "
Next
Erase arrWords
AddLinks=result
End Function
Function IsURL(strText)
Dim arrTmp
IsURL=True
If BeginsWith(strText, "http://") Then Exit Function
If BeginsWith(strText, "https://") Then Exit Function
arrTmp=Split(strText, ".")
If UBound(arrTmp)>1 Then
If FindInArray(arrTmp, " ")=0 Then
If IsValidDomain( arrTmp(UBound(arrTmp)) ) Then
Erase arrTmp
Exit Function
End If
End If
End If
Erase arrTmp
IsURL=False
End Function
Function MakeFullURL(strText)
Dim result
If (Not(BeginsWith(strText, "http://"))) And (Not(BeginsWith(strText, "https://"))) Then
result = "http://"
End If
result = result & strText
MakeFullURL = result
End Function
Function BeginsWith(strBig, strSmall)
BeginsWith = (Left(LCase(strBig), Len(strSmall))=LCase(strSmall))
End Function
Function FindInArray(arr, strToFind)
Dim x, result
result=0
For x=0 To UBound(arr)
If InStr(1, arr(x), strToFind, 1)>0 Then result=result+1
Next
FindInArray=result
End Function
Function IsValidDomain(str)
'optional.... if you want, have list of all available domains and compare.
IsValidDomain=True
End Function
Dim strText
strText="hello please go to http://www.google.com and visit us again at ynet.co.il"
Response.Write(AddLinks(strText))
%>
|