ASP的一些常用函数!

原创 2004年06月26日 13:14:00

<%
'===========================================================================
' HANDY ASP FUNCTIONS
' By: Dave Nicoll (dave@caelan.net) and Paul Roberts (paul@caelan.net)
' Web site: www.caelan.net
' Updated: 24/06/2002
' Abbreviations used: str:string, int:integer, b:boolean, csv:Comma seperated values
'===========================================================================
' Function name reference -
'   CleanID
'   CleanString
'   IsValidEmail
'   PrepStringFromDB
'   AddLeadingZero
'   highlightPhrase
'   IsANumber
'   DeleteFile
'   FileExists
'   ReverseFormatYear
'   getAge
'   newGUID
'   containsBadWords
'   removeBadWords
'   CreateThumbnail
'   rgbtohex
'   NTUsername
'===========================================================================

' Input: "1055; DELETE FROM tblArticles"    --the user tried to inject some sql
' Output: -1
' Useful for: checking an id passed through the querystring doesn't contain an SQL injection
function CleanID(intID)
 on error resume next
 CleanID = CLng(intID)
 if err.number<>0 then
  CleanID=-1 'return -1 as an error code
 end if
 on error goto 0
end function

' Input: "isn't this great?"
' Output: "isn''t this great?"
' Useful for: escaping characters before inserting in the database
'''' 添加到数据库中过滤
function CleanString(strString)
 CleanString = Replace(strString, "'", "''")
 CleanString = Replace(CleanString, "|", "/") 
end function

' Input: "bob@bob.com"
' Output: true
' Useful for: making sure users enter a syntactically correct email address
function IsValidEmail(strEmailAddress)
 if len(strEmailAddress)>=6 and instr(strEmailAddress,".") and instr(strEmailAddress,"@") then IsValidEmail=true else IsValidEmail = false
end function

' Input: "Copyright (c) 2002 Caelan"
' Output: "Copyright &copy; 2002 Caelan"
' Useful for: converting returned db data into safe html
function PrepStringFromDB(strString)
 strString = Replace(strString, "<", "&lt;") 
 strString = Replace(strString, ">", "&gt;")
 strString = Replace(strString, "(c)", "&copy;")
 strString = Replace(strString, VbCrlf, "<BR>")
 strString = Replace(strString, """", "&quot;")
 strString = Replace(strString, "'", "&39;")
 PrepStringFromDB = strString
end function

' Input: "7",2
' Output: "007"
' Useful for: inserting leading zeros before times and dates
function AddLeadingZero(strString, num)
 if num=>Len(strString) then
  For i = len(strString) to num -1
   strString = "0" & strString
  Next
 end if
 AddLeadingZero = strString
end function

' Input: "keyword","This is a body of text including keywords."
' Output: "This is a body of text including <strong>keyword</strong>s."
function highlightPhrase(searchString,strBody)
 strarray = split(searchString,".")
 set re = New RegExp 
 re.Global = True
 re.IgnoreCase = True
 're.MultiLine = True 
 for i = 0 to ubound(strarray)
  re.Pattern = "(" & strarray(i) & "+)"
  strBody = re.Replace(strBody,"<strong>$1</strong>")
 next  
 highlightPhrase = strBody
end function

' Input: 542
' Output: true
' Useful for: checking if a value passed through the querystring is a number
function IsANumber(intNumber)
 if isNull(intNumber) or intNumber ="" Then
  IsANumber = False
 elseif IsNumeric(intNumber) Then
  if intNumber>999999999 or intNumber <-999999999 Then
   IsANumber = False
  else
   IsANumber = True
  end if
 else
  IsANumber = False
 end if
end function

' Input: "/uploads/file23002.zip"
' Output: true/false
' Useful for: deleting files on the server
function DeleteFile(strFileName)
 Set FSO = CreateObject("Scripting.FileSystemObject")
 strFileName = server.mappath(strFileName)
 On Error Resume Next
 FSO.DeleteFile(strFilename)
 If Err.Number<>0 Then
  DeleteFile = False
 Else
  DeleteFile= True
 end if
 On Error Goto 0
 Set FSO = Nothing
end function

' Input: "/images/file23002.zip"
' Output: true/false
' Useful for: checking if a file exists on the server
function FileExists(strFileName)
 Set FSO = CreateObject("Scripting.FileSystemObject")
 strFileName = server.mappath(strFileName)
 If FSO.FileExists(strFilename) then FileExists = True Else FileExists = False
 Set FSO = Nothing
end Function

' Input: 24/06/2002 15:55
' Output: 2002/6/24 15:55
' Useful for: reversing date formats so that it works in all language formats. REALLY HANDY!! :)
function ReverseFormatYear(inputdate)
 minutenumber = minute(inputdate)
 hournumber = hour(inputdate)
 daynumber = day(inputdate)
 monthnumber = month(inputdate)
 yearnumber = year(inputdate)
 outputdate = yearnumber & "/" & monthnumber & "/" & daynumber & " " & hournumber & ":" & minutenumber
 ReverseFormatYear = outputdate
end Function

' Input: "05/10/1977"
' Output: "24"
' Useful for: working out someones age
function getAge(strDOB)
 Dim strYears
 strYears = Year(Date) - Year(strDOB)
 If Month(strDOB) > Month(Date) Then
  getAge = strYears - 1
 ElseIf Month(strDOB) < Month(Date) Then
  getAge = strYears
 ElseIf Day(strDOB) <= Day(Date) Then
  getAge = strYears
 Else
  getAge = strYears - 1
 End If
end function

' Requires: GuidMakr (http://www.google.co.uk/search?q=guidmakr) to be installed on the server
' Output: {19522AF6-3E0C-475F-AAEA-474EC34C77A2}  -- random every time obviously ;)
' Useful for: making a unique ID
function newGUID()
  Set MyGuid = Server.CreateObject ("GuidMakr.GUID")
  newGUID=(MyGuid.GetGUID)
  Set MyGuid = Nothing
end function

' Input: "this is a test string containing no banned words"
' Output: false
' Useful for: detecting swear words
function containsBadWords(strInputString)
 'Use sparingly as instr is a slow function
 arrBadWords=split("fuck,shit,cunt,pussy,twat,penis,vagina,nazi,hitler,bastard,minge,whore,wank",",")
 for i=0 to ubound(arrBadWords)
  if instr(strInputString,arrBadWords(i)) then
   containsBadWords=true
   exit function
  end if
 next
 containsBadWords=false
end function

' Input: "this fucking string contains the word fuck."
' Output: "this ****ing string contains the word ****."
' Useful for: filtering out swear words
function removeBadWords(strInputString) 
   arrBadWords=split("fuck,shit,cunt,pussy,twat,penis,vagina,nazi,hitler,bastard,minge,whore,wank",",")
   for i = 0 to ubound(arrBadWords)
  strInputString= replace(strInputString, arrBadWords(i), string(len(arrBadWords(i)),"*"), 1,-1,1)
   next
 removeBadWords = strInputString
end function

' Input: "c:/images", "c:/thumbnails", "filename.jpg", 120, 120
' Output: jpeg file
' Useful for: Creating a thumbnail of a larger image
' By: Dave Nicoll (used from ASPGallery under license)
' Requires: ASPImage from ServerObjects Inc (www.serverobjects.com)
function CreateThumbnail(strPath, strOutputPath, strFilename, maxX, maxY)  'Paths should be passed as mapped paths, i.e. c:/inetpub/wwwroot/mysite/images
 Set image = server.createobject("aspimage.image")
 if image.loadimage(strPath & "/" & strFilename) then
  intWidth    = image.MaxX
  intHeight   = image.MaxY

  if intWidth>intHeight then
   intRatio=(maxX/intWidth)
  else
   intRatio=(maxY/intHeight)
  end if
  image.resizeR intWidth*intRatio, intHeight*intRatio
  
  Image.ImageFormat = 1
  Image.PixelFormat = 6
  strSfilename=split(strFilename,".")
  Image.Filename=strOutputPath & "/" & strSfilename(0)
  if Image.SaveImage then
   Set Image = nothing
   CreateThumbnail=true 'the operation was successful, return true
  else
   'Error while saving, abort
   'response.write "Error: couldn't save image " & strOutputPath & "/" & strFilename & "<br>" & vbcrlf
   CreateThumbnail = false 'operation failed, return false
   Set Image = nothing
   'response.end
  end if
 else
  'Error while loading, abort
  'response.write "Error: couldn't load image " & strPath & "/" & strFilename & "<br>" & vbcrlf
  CreateThumbnail = false 'operation failed, return false
  Set Image = nothing
  'response.end
  exit function
 end if
end function

' Input: "150,60,120"
' Output: "78326E"
' Useful for: converting rgb values into their hex code
Function rgbtohex(csvColor)
 rgbValues=split(csvColor,",")
 for i = 0 to ubound(rgbValues)
  If Len(Hex(rgbValues(i))) = 1 Then
   rgbtohex = rgbtohex & "0" & Hex(rgbValues(i))
  Else
   rgbtohex = rgbtohex & Hex(rgbValues(i))
  End If
 next
End Function

' Output: "ntusername"
' Useful for: getting the NT username of the authenticated user
function NTUsername()
 on error resume next
 arrMyArray = split(Request.ServerVariables("LOGON_USER"),"/")
 NTUsername = lcase(arrSomething(1))
 if err.number<>0 then
  NTUsername=-1 'return -1 as an error code
 end if
 on error goto 0
end function

%>

 

asp 中常用的文件处理函数

asp 中常用的文件处理函数 asp 中处理文件上传以及删除时常用的自定义函数 ''''''''''''''''''''''''''''''''''''''''''''''''...
  • u014739765
  • u014739765
  • 2014年04月23日 16:51
  • 254

ASP写的汉字转换UTF-8及UTF-8转GB2312

汉字转换为UTF-8 function chinese2unicode(Str)   dim i   dim Str_one   dim Str_unicode   for i=1 to l...
  • hefeng_aspnet
  • hefeng_aspnet
  • 2013年09月26日 15:29
  • 2312

ASP站点开发常用函数总结

ASP站点开发常用函数总结
  • netuser1937
  • netuser1937
  • 2016年12月19日 13:44
  • 277

asp网站基本构建(常用数据库的连接语句)

一、数据库的初始连接 ASP的对象存取数据库方法 在ASP中,用来存取数据库的对象统称ADO(Active Data Objects),主要含有三种对象:Connection、Recordset ...
  • form88
  • form88
  • 2017年03月26日 20:05
  • 575

asp短信接口开发经验及具体开发实现

http://www.leiyusoft.com/sms_sdk_asp.html 官网介绍及Demo下载。
  • baidu_31002735
  • baidu_31002735
  • 2015年09月02日 08:33
  • 769

vb常用函数一览表

vb常用函数一览表   常用内部函数 数学函数 函数 功能 示例 结果 说明 Ab...
  • u014451076
  • u014451076
  • 2014年11月23日 00:26
  • 970

asp与php常用函数对照表(全)

asp与php常用函数对照表(全)  (2013-02-20 14:13:44) 转载▼ 标签:  对照表   函数   it 分类: p...
  • zhangfeng1133
  • zhangfeng1133
  • 2015年10月30日 23:26
  • 335

PHP常用函数归类总结【大全】

用函数归类总结, 学习了这么就PHP,基础知识总感觉不牢靠,尤其是数组,字符串函数的应用,抽了个时间总结了一下,已做日后备查。 一。PHP基础语法 变量,常量     严格区分大小写,但内置结...
  • ty_hf
  • ty_hf
  • 2015年11月04日 18:57
  • 7982

Asp.Net 如何调用js中的函数function ?

http://bbs.csdn.net/topics/330215458 1、直接在前台调用 javascript 函数  很简单,在 head 元素之间加入 script 元素,将 ty...
  • DJ2008
  • DJ2008
  • 2013年09月28日 17:34
  • 5434

ASP远程脚本调用

远程脚本调用技术(Remote Scripting)的最大特点是:客户端脚本调用服务器脚本时无需刷新整个页面。一般情况  下,Web页面需要与服务器交互时必须提交表单数据,刷新整个页面。这不仅不利于...
  • u014739784
  • u014739784
  • 2014年04月25日 09:35
  • 386
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ASP的一些常用函数!
举报原因:
原因补充:

(最多只允许输入30个字)