Blog: Tag Archives: ASP

ASP – RegEx – Strip Special Characters from string

Posted On:

Function CleanURL(URLString)

	IF INSTR(URLString,"-")=>1 THEN
		URLString = Replace(URLString, "-","")
	END IF
	IF INSTR(URLString, " ")=>1 THEN
		URLString = Replace(URLString, " ", "-")
	END IF

		Set RegularExpressionObject = New RegExp
	With RegularExpressionObject
		.Pattern="[^a-zA-Z0-9\-\][:punct:]"
		.IgnoreCase = True
		.Global = True
	End With
	TempURL = RegularExpressionObject.Replace(URLString, "")
	
	IF INSTR(TempURL, "--")=>1 THEN
		TempURL = Replace(TempURL, "--", "")
	END IF
	IF Right(TempURL, 1)="-" THEN
		TempURL = Left(TempURL, Len(TempURL)-1)
	END IF
		CleanURL = TempURL

	Set RegularExpressionObject = nothing
End Function
		

strText = "This Is My $1000 Dollar URL-Baby, ""Yeah:""! 'Whoo' @ £ &"
Response.Write "Safe :"&CleanURL(strText)

ASP – Get a Random file from folder

Posted On:

<% 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set fold = fso.GetFolder(Server.MapPath("folderName")) 
    Set fileset = fold.files 
    fileCount = fileset.count 
 
    if fileCount > 0 then 
 
        counter = 1 
        randomize 
        fileToPick = clng((rnd * fileCount) + 0.5) 
 
        for each file in fileset 
            if counter < fileToPick then 
                counter = counter + 1 
            else 
                randomFile = file.name 
                exit for 
            end if 
        next 
 
        response.write randomFile 
 
    else 
 
        response.write "Empty folder." 
 
    end if 
 
    set fileset = nothing 
    set fold = nothing 
    set fso = nothing 
%>

ASP – Generate a CSV file on the fly

Posted On:

strOutPut = "Name, Email Address"
IF ListType="" THEN
	strOutPut = strOutPut&", Product News, Company News, Special Offers"
END IF

strOutPut = strOutPut&vbcrlf


	DO WHILE NOT objRecordSet.EOF
	strOutPut = strOutPut & objRecordSet("Mailing_Name")&","&objRecordSet("Mailing_EmailAddress")
	IF ListType="" THEN
		IF objRecordSet("Mailing_ProductNews")=1 THEN
			strOutPut=strOutPut&",Y, "
		ELSE	
			strOutPut=strOutPut&",N, "
		END IF
		IF objRecordSet("Mailing_CompanyNews")=1 THEN
			strOutPut=strOutPut&"Y, "
		ELSE	
			strOutPut=strOutPut&"N, "
		END IF
		IF objRecordSet("Mailing_SpecialOffers")=1 THEN
			strOutPut=strOutPut&"Y"
		ELSE	
			strOutPut=strOutPut&"N"
		END IF
	END IF	
		strOutPut=strOutPut&vbcrlf
	objRecordSet.MoveNext
	LOOP
	objRecordSet.Close

Set objRecordSet = nothing


Response.Clear
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "filename=" & FileName & ";"
Response.Write(strOutPut)

ASP – Sort An Array – Bubblesort

Posted On:

ArTxt="436-452-95"

Dim arrValue 
arrValue = Split(ArTxt,"-")
For i = 0 TO Ubound(arrValue)
Response.Write i&" "&CINT(arrValue(i))&"<br>"
NEXT

Function BubbleSort(arrInt)
for i = UBound(arrInt) - 1 To 0 Step -1
    for j= 0 to i
        if Cint(arrInt(j))>Cint(arrInt(j+1)) then
            temp = Cint(arrInt(j+1))
           arrInt(j+1) = Cint(arrInt(j))
            arrInt(j) = temp
        end if
    next
next 
BubbleSort = arrInt
End Function
response.write Join(BubbleSort(arrValue),"-")

ASP – Strip Text between two known tags

Posted On:


'=== extractor function
'=== strSrc: the text to pull from
'=== strLft: left marker to start at
'=== strRgt: right marker to end at
function parser(strSrc, strLft, strRgt)
        dim strParsed
        strLft = cStr(strLft)
       
        if strLft = "1" then '=== start from beginning
                if inStr(strSrc, strRgt) > 0 then strParsed = left( strSrc, inStr(strSrc, strRgt) - 1)   
        elseif strLft = "" then '=== start from right marker (trim out last result)
                if inStr(strSrc, strRgt) > 0 then strParsed = right( strSrc, len(strSrc) - (inStr(strSrc, strRgt) + len(strRgt) - 1))
        else
                if inStr(strSrc, strLft) > 0 then
                        strParsed = right( strSrc, len(strSrc) - (inStr(strSrc, strLft) + len(strLft) - 1))
                        if inStr(strSrc, strRgt) > 0 then strParsed = left( strParsed, inStr(strParsed, strRgt) - 1)
                end if
        end if
       
        strParsed = trim(strParsed)
       
        do until left(strParsed, 1) <> chr(10) and right(strParsed, 1) <> chr(10)
                if left(strParsed, 1) = chr(10) then strParsed = right(strParsed, len(strParsed) - 1)
                if right(strParsed, 1) = chr(10) then strParsed = left(strParsed, len(strParsed) - 1)
        loop
       
        strParsed = replace( strParsed, "%20", " ")
        strParsed = trim(strParsed)
       
        parser = strParsed
end function

strQuery = "SELECT * FROM tblCactuShopOrderNumbers WHERE ON_Details LIKE '%Card Number: 0%'"
objRecordSet.Open strQuery, objDataConn, 1, 2, 1
DO WHILE NOT objRecordSet.EOF
strOrderDetails = objRecordSet("ON_Details")
text=parser(strOrderDetails, "<CardDetails>", "</CardDetails>")
strOrderDetails=Replace(strOrderDetails, text, "*** Card Details Deleted For Security Reasons ***")
objRecordSet("ON_Details")=strOrderDetails
objRecordSet.Update
objRecordSet.MoveNext
LOOP
objRecordSet.close

ASP – CDO Email Script

Posted On:

 <!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->
<%
Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mail.katywebdev.com"
.Update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = "mail@katywebdev.com"
.To = "k.whitton@pindar.com"
.Subject = "Sample CDO Message"
.TextBody = "This is a test for CDO.message"
.Send
End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>
Done