Feb 09 2010

ASP equivalent to PHP strip_tags

Category: Aspadmin @ 10:09 am

I’ve found those functions around in the internet and I put them here just to remind how to strip tags with ASP. In ASP there isn’t an equivalent of PHP strip_tags function, so, here there are two function that do this with regular expressions. The first function strips anything:

function strip_tags(strHTML)
	dim regEx
	Set regEx = New RegExp
	With regEx
		.Pattern = "<(.|\n)+?>"
		.IgnoreCase = true
		.Global = true
	End With
	strip_tags = regEx.replace(strHTML, "")
end function

The second one has an “allowed tags” parameter as the PHP function to keep some specified tags (the tags to keep must be comma separated). This second function is better since you can put allowedTags equal to an empty string to strip all the tags as the first function.

function strip_tags(strHTML, allowedTags)

	dim objRegExp, strOutput
	set objRegExp = new regexp

	strOutput = strHTML
	allowedTags = "," & lcase(replace(allowedTags, " ", "")) & ","

	objRegExp.IgnoreCase = true
	objRegExp.Global = true
	objRegExp.MultiLine = true
	objRegExp.Pattern = "<(.|\n)+?>"
	set matches = objRegExp.execute(strHTML)
	objRegExp.Pattern = "<(/?)(\w+)[^>]*>"
	for each match in matches
		tagName = objRegExp.Replace(match.value, "$2")
		if instr(allowedTags, "," & lcase(tagName) & ",") = 0 then
			strOutput = replace(strOutput, match.value, "")
		end if
	next
	strip_tags = strOutput
	set objRegExp = nothing
end function
  • Share/Bookmark

Tags: , , ,


Feb 09 2010

ASP Function to count files in a folder

Category: Aspadmin @ 9:50 am

This ASP function counts files of a specified extension inside a folder: this is done by creating a file system object, open folder and scan files for matching the extension. The match of the extension is made without regular expression but using string functions: Right, Len and Ucase.

Function countFiles(path,ext)
	Dim fs, folder, c
	c = 0
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set folder = fs.GetFolder(path)
	For Each item In folder.Files
		If UCase(Right(item.Name, Len (ext))) = UCase(ext)) Then c=c+1
	Next
	countFiles = c
End Function
  • Share/Bookmark

Tags: , , ,


Dec 23 2009

ASP web bot that get exchange rates

Category: Asp, Spiders & webbotsadmin @ 3:06 pm

Thanks to Bank of Italy (Banca d’Italia), there are free exchange rates data that are easy to retrieve and parse, here is the code of a mini webbot written in ASP that goes out on Bank of Italy site, get the exchanges rates and print them. It’s very easy to customize and use it (since there are also historical data you can also make graphs like this).

exchange rates graph

<%
Dim yesterday
yesterday = dateadd("d",-1,date)

Dim objHttp
Set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
objHttp.Open "GET", "http://uif.bancaditalia.it/UICFEWebroot/QueryOneDateAllCur?lang=en&rate=0&initDay=" & Day(yesterday) & "&initMonth=" & Month(yesterday) & "&initYear=" & Year(yesterday) & "&refCur=euro&R1=csv", False
objHttp.Send
If objHttp.Status = 200 Then
	Dim arrLines
	Dim arrFields

	Dim i
	arrLines = Split(objHttp.ResponseText,Chr(10))
	For i = 0 To UBound(arrLines)-1
		If i >= 4 then
			arrFields = Split(arrLines(i),",")
			response.write arrFields(4) & " " & arrFields(1) & " (" & arrFields(2) & ") per 1 EUR<br/>"
		End if
	Next
Else
	Response.Write(objHttp.Status & " - " & objHttp.StatusText)
End If
Set objHttp = Nothing
%>
  • Share/Bookmark

Tags: , , ,


Next Page »