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: , , ,


Feb 03 2010

Fix html tags, close tags, repair bad quotes and more

Category: Html, Phpadmin @ 2:31 pm

This class can solve many problems coming from user generated html content or to fix html content before making some hard work with your bots! (It’s specially usefull for web sites without the Html Tidy module of PHP).

Hre is a quick list of the magic things it can do.

  1. delete closed tags without their opening tag
  2. fix open tag without close, closing them automatically
  3. check bad nesting and fix them (if you have a bold inside a bold… or a paragrah that contains a table…)
  4. fix bad quotes in attributes (open quotes where missing…)
  5. merge different styles attributes in the same tag
  6. remove html comments
  7. remove empty tags and more bad tags

It works ina complex way since it analyzes the html code char by char and search for tags. When a tag is found start the work of cleaning attributes, then store data found in a matrix and search for the closing tags.
The data saved in the matrix are later used to re-build the correct fixed html.

EXAMPLE:
It’s very simple to use, suppose you have a variable with the dirty html:

$a = new HtmlFixer();
$clean = $a->getFixedHtml($dirty_html);

You can download the class from the HTML FIXER page.

  • Share/Bookmark

Tags: , , , , , ,


Next Page »