Feb 09 2010

ASP equivalent to PHP strip_tags

Category: AspGiulio Pons @ 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: , , ,


Jan 06 2010

Test if a remote url exists with PHP and CURL

Category: Php,Spiders & webbotsGiulio Pons @ 10:13 am

If you have to test if a local file exists you will probably use the php file_exists function, but if you have to test a remote file, that is to say a remote url, than you can use CURL and get the headers returned by the http request. If you receive a 200 code, than it’s ok, else the url is not correct.

This function is included in the Mini Bots Class.

function url_exists($url) {
	$ch = @curl_init($url);
	@curl_setopt($ch, CURLOPT_HEADER, TRUE);
	@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
	@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$status = array();
	preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($ch) , $status);
	return ($status[1] == 200);
}

If you you don’t have CURL lib istalled you can use the php get_headers function, it returns an array with the headers:

$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));

If you apply the preg_match function to the first element of the array you will reach the same result:

function url_exists($url) {
	$h = get_headers($url);
	$status = array();
	preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
	return ($status[1] == 200);
}
  • Share/Bookmark

Tags: , , , , , ,