asp常用函数
<%
'**********************************************
'strLength函数:计算字符串的长度
'中文字符统计为两个单位长
'**********************************************
dim WINNT_CHINESE
function strLength(str)
ON ERROR RESUME NEXT
dim WINNT_CHINESE
WINNT_CHINESE = (len(""论坛"")=2)
if WINNT_CHINESE then
dim l,t,c
dim i
l=len(str)
t=l
for i=1 to l
c=asc(mid(str,i,1))
if c<0 then c=c+65536
if c>255 then
t=t+1
end if
next
strLength=t
else
strLength=len(str)
end if
if Err.number<>0 then Err.clear
end function
'**********************************************************
'isInteger函数:判断是否为整数(参数数字与字符串都可)
'**********************************************************
function isInteger(para)
on error resume next
dim str
dim l,i
if isNUll(para) then
isInteger=false
exit function
end if
str=cstr(para)
if trim(str)="""" then
isInteger=false
exit function
end if
l=len(str)
for i=1 to l
if mid(str,i,1)>""9"" or mid(str,i,1)<""0"" then
isInteger=false
exit function
end if
next
isInteger=true
if err.number<>0 then err.clear
end function
'*************************************************
'cutStr函数:截取前面几个字符,后面添加""""号
'没有区分中文字符长度,即一个中文字长度为1
'*************************************************
function cutStr(str,strlen)
dim l,t,c
l=len(str)
t=0
if l<>0 then
for j=1 to l
c=Abs(Asc(Mid(str,j,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
cutStr=HTMLEncode2(left(str,j)&"""")
exit for
else
cutStr=HTMLEncode2(str)
end if
next
else
cutStr="" ""
end if
end function
'*******************************************************************************************
'browser函数:判断浏览器类型(参数info使用request.ServerVariables(""HTTP_USER_AGENT"")调用)
'*******************************************************************************************
function browser(info)
if Instr(info,""MSIE 5.5"")>0 then
browser=""浏 览 器:Internet Explorer 5.5""
elseif Instr(info,""MSIE 6.0"")>0 then
browser=""浏 览 器:Internet Explorer 6.0""
elseif Instr(info,""MSIE 5.01"")>0 then
browser=""浏 览 器:Internet Explorer 5.01""
elseif Instr(info,""MSIE 5.0"")>0 then
browser=""浏 览 器:Internet Explorer 5.00""
elseif Instr(info,""MSIE 4.0"")>0 then
browser=""浏 览 器:Internet Explorer 4.01""
else
browser=""浏 览 器:未知""
end if
end function
'************************************************************************************************
'system函数:判断客户端操作系统类型(参数info使用request.ServerVariables(""HTTP_USER_AGENT"")调用)
'************************************************************************************************
function system(info)
if Instr(info,""NT 5.1"")>0 then
system=system+""操作系统:Windows XP""
elseif Instr(info,""NT 5"")>0 then
system=system+""操作系统:Windows 2000""
elseif Instr(info,""NT 4"")>0 then
system=system+""操作系统:Windows NT4""
elseif Instr(info,""98"")>0 then
system=system+""操作系统:Windows 98""
elseif Instr(info,""95"")>0 then
system=system+""操作系统:Windows 95""
else
system=system+""操作系统:未知""
end if
end function
'***************************************************************
'chkemail函数:检验email的有效性(参数strEmailAddr为email地址)
'结果:true 或 false
'***************************************************************
Function chkemail(strEmailAddr)
Dim re
if strEmailAddr<>"""" then
Set re = new RegExp
re.pattern = ""^[A-Za-z0-9_.-]+@([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,4}$""
chkemail=re.Test(strEmailAddr)
else
chkemail=true
end if
end function
'****************************************************************
'chkoicq函数:检验oicq的有效性(参数oicq为oicq号)
'结果:true 或 false
'****************************************************************
Function chkoicq(oicq)
Dim re1
if oicq<>"""" then
Set re1 = new RegExp
re1.IgnoreCase = false
re1.global = false
re1.Pattern = ""[0-9]{4,10}$""
chkoicq = re1.Test(oicq)
else
chkoicq=true
end if
End Function
'****************************************************************************
'DateToStr函数:把日期转化为字符串系列(参数dtDateTime为日期字符串或日期型)
'结果:年月日时分秒
'****************************************************************************
function DateToStr(dtDateTime)
DateToStr = year(dtDateTime) & doublenum(Month(dtdateTime)) & doublenum(Day(dtdateTime)) & _
doublenum(Hour(dtdateTime)) & doublenum(Minute(dtdateTime)) & doublenum(Second(dtdateTime)) & """"
end function
'****************************************************************************
'doublenum函数:把单位数转化为两位数,保持日期位数统一(由DateToStr调用)
'结果:双位数
'****************************************************************************
function doublenum(fNum)
if fNum > 9 then
doublenum = cstr(fNum)
else
doublenum = ""0"" & cstr(fNum)
end if
end function
'****************************************************************************
'StrToDate函数:把参数year,month,day转化为日期(""year-month-day"")
'结果:日期
'****************************************************************************
function StrToDate(year,month,day)
StrToDate=cstr(year) & ""-"" & cstr(month) & ""-"" & cstr(day)
end function
'************************************************************************************
'ChkBadWords函数:过滤不良语句(参数BadWords为不良语句定义,fString为要检验的语句)
'结果:将不良语句用*号代替输出
'************************************************************************************
function ChkBadWords(BadWords,fString)
if not(isnull(BadWords) or isnull(fString)) then
bwords = split(BadWords, ""|"")
for i = 0 to ubound(bwords)
fString = Replace(fString, bwords(i), string(len(bwords(i)),""*""))
next
ChkBadWords = fString
end if
end function
'***************************************************************************
'HTMLEncode函数:将HTML语句转为字符型输出(参数fString为HTML语句)
'
'***************************************************************************
function HTMLEncode(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, CHR(32), "" "")
fString = Replace(fString, CHR(34), """"")
fString = Replace(fString, CHR(39), ""'"")
fString = Replace(fString, CHR(13), """")
fString = Replace(fString, CHR(10) & CHR(10), ""</P><P> "")
fString = Replace(fString, CHR(10), "" <BR> "")
HTMLEncode = fString
end if
end function
'***************************************************************************
'HTMLEncode函数:将HTML语句转为字符型输出(参数fString为HTML语句)
'
'***************************************************************************
function HTMLEncode2(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, CHR(32), "" "")
fString = Replace(fString, CHR(34), """"")
fString = Replace(fString, CHR(39), ""'"")
fString = Replace(fString, CHR(13), """")
HTMLEncode2 = fString
end if
end function
'********************************************************************
'HTMLDecode函数:将语句转为HTML输出(参数fString为字符语句)
'********************************************************************
function HTMLDecode(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, "" "",CHR(32))
fString = Replace(fString, """"", CHR(34))
fString = Replace(fString, ""'"",CHR(39))
fString = Replace(fString, """", CHR(13))
fString = Replace(fString, ""</P><P>"", CHR(10) & CHR(10))
fString = Replace(fString, ""<BR>"", CHR(10))
HTMLDecode = fString
end if
end function
'***********************************************
'repleatBlank函数:自动生成n个空格
'***********************************************
function repleatBlank(n)
n=cint(n)
for u=1 to n
repleatBlank=repleatBlank & "" ""
next
end function
%>
'**********************************************
'strLength函数:计算字符串的长度
'中文字符统计为两个单位长
'**********************************************
dim WINNT_CHINESE
function strLength(str)
ON ERROR RESUME NEXT
dim WINNT_CHINESE
WINNT_CHINESE = (len(""论坛"")=2)
if WINNT_CHINESE then
dim l,t,c
dim i
l=len(str)
t=l
for i=1 to l
c=asc(mid(str,i,1))
if c<0 then c=c+65536
if c>255 then
t=t+1
end if
next
strLength=t
else
strLength=len(str)
end if
if Err.number<>0 then Err.clear
end function
'**********************************************************
'isInteger函数:判断是否为整数(参数数字与字符串都可)
'**********************************************************
function isInteger(para)
on error resume next
dim str
dim l,i
if isNUll(para) then
isInteger=false
exit function
end if
str=cstr(para)
if trim(str)="""" then
isInteger=false
exit function
end if
l=len(str)
for i=1 to l
if mid(str,i,1)>""9"" or mid(str,i,1)<""0"" then
isInteger=false
exit function
end if
next
isInteger=true
if err.number<>0 then err.clear
end function
'*************************************************
'cutStr函数:截取前面几个字符,后面添加""""号
'没有区分中文字符长度,即一个中文字长度为1
'*************************************************
function cutStr(str,strlen)
dim l,t,c
l=len(str)
t=0
if l<>0 then
for j=1 to l
c=Abs(Asc(Mid(str,j,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
cutStr=HTMLEncode2(left(str,j)&"""")
exit for
else
cutStr=HTMLEncode2(str)
end if
next
else
cutStr="" ""
end if
end function
'*******************************************************************************************
'browser函数:判断浏览器类型(参数info使用request.ServerVariables(""HTTP_USER_AGENT"")调用)
'*******************************************************************************************
function browser(info)
if Instr(info,""MSIE 5.5"")>0 then
browser=""浏 览 器:Internet Explorer 5.5""
elseif Instr(info,""MSIE 6.0"")>0 then
browser=""浏 览 器:Internet Explorer 6.0""
elseif Instr(info,""MSIE 5.01"")>0 then
browser=""浏 览 器:Internet Explorer 5.01""
elseif Instr(info,""MSIE 5.0"")>0 then
browser=""浏 览 器:Internet Explorer 5.00""
elseif Instr(info,""MSIE 4.0"")>0 then
browser=""浏 览 器:Internet Explorer 4.01""
else
browser=""浏 览 器:未知""
end if
end function
'************************************************************************************************
'system函数:判断客户端操作系统类型(参数info使用request.ServerVariables(""HTTP_USER_AGENT"")调用)
'************************************************************************************************
function system(info)
if Instr(info,""NT 5.1"")>0 then
system=system+""操作系统:Windows XP""
elseif Instr(info,""NT 5"")>0 then
system=system+""操作系统:Windows 2000""
elseif Instr(info,""NT 4"")>0 then
system=system+""操作系统:Windows NT4""
elseif Instr(info,""98"")>0 then
system=system+""操作系统:Windows 98""
elseif Instr(info,""95"")>0 then
system=system+""操作系统:Windows 95""
else
system=system+""操作系统:未知""
end if
end function
'***************************************************************
'chkemail函数:检验email的有效性(参数strEmailAddr为email地址)
'结果:true 或 false
'***************************************************************
Function chkemail(strEmailAddr)
Dim re
if strEmailAddr<>"""" then
Set re = new RegExp
re.pattern = ""^[A-Za-z0-9_.-]+@([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,4}$""
chkemail=re.Test(strEmailAddr)
else
chkemail=true
end if
end function
'****************************************************************
'chkoicq函数:检验oicq的有效性(参数oicq为oicq号)
'结果:true 或 false
'****************************************************************
Function chkoicq(oicq)
Dim re1
if oicq<>"""" then
Set re1 = new RegExp
re1.IgnoreCase = false
re1.global = false
re1.Pattern = ""[0-9]{4,10}$""
chkoicq = re1.Test(oicq)
else
chkoicq=true
end if
End Function
'****************************************************************************
'DateToStr函数:把日期转化为字符串系列(参数dtDateTime为日期字符串或日期型)
'结果:年月日时分秒
'****************************************************************************
function DateToStr(dtDateTime)
DateToStr = year(dtDateTime) & doublenum(Month(dtdateTime)) & doublenum(Day(dtdateTime)) & _
doublenum(Hour(dtdateTime)) & doublenum(Minute(dtdateTime)) & doublenum(Second(dtdateTime)) & """"
end function
'****************************************************************************
'doublenum函数:把单位数转化为两位数,保持日期位数统一(由DateToStr调用)
'结果:双位数
'****************************************************************************
function doublenum(fNum)
if fNum > 9 then
doublenum = cstr(fNum)
else
doublenum = ""0"" & cstr(fNum)
end if
end function
'****************************************************************************
'StrToDate函数:把参数year,month,day转化为日期(""year-month-day"")
'结果:日期
'****************************************************************************
function StrToDate(year,month,day)
StrToDate=cstr(year) & ""-"" & cstr(month) & ""-"" & cstr(day)
end function
'************************************************************************************
'ChkBadWords函数:过滤不良语句(参数BadWords为不良语句定义,fString为要检验的语句)
'结果:将不良语句用*号代替输出
'************************************************************************************
function ChkBadWords(BadWords,fString)
if not(isnull(BadWords) or isnull(fString)) then
bwords = split(BadWords, ""|"")
for i = 0 to ubound(bwords)
fString = Replace(fString, bwords(i), string(len(bwords(i)),""*""))
next
ChkBadWords = fString
end if
end function
'***************************************************************************
'HTMLEncode函数:将HTML语句转为字符型输出(参数fString为HTML语句)
'
'***************************************************************************
function HTMLEncode(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, CHR(32), "" "")
fString = Replace(fString, CHR(34), """"")
fString = Replace(fString, CHR(39), ""'"")
fString = Replace(fString, CHR(13), """")
fString = Replace(fString, CHR(10) & CHR(10), ""</P><P> "")
fString = Replace(fString, CHR(10), "" <BR> "")
HTMLEncode = fString
end if
end function
'***************************************************************************
'HTMLEncode函数:将HTML语句转为字符型输出(参数fString为HTML语句)
'
'***************************************************************************
function HTMLEncode2(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, CHR(32), "" "")
fString = Replace(fString, CHR(34), """"")
fString = Replace(fString, CHR(39), ""'"")
fString = Replace(fString, CHR(13), """")
HTMLEncode2 = fString
end if
end function
'********************************************************************
'HTMLDecode函数:将语句转为HTML输出(参数fString为字符语句)
'********************************************************************
function HTMLDecode(fString)
if not isnull(fString) then
fString = replace(fString, "">"", "">"")
fString = replace(fString, ""<"", ""<"")
fString = Replace(fString, "" "",CHR(32))
fString = Replace(fString, """"", CHR(34))
fString = Replace(fString, ""'"",CHR(39))
fString = Replace(fString, """", CHR(13))
fString = Replace(fString, ""</P><P>"", CHR(10) & CHR(10))
fString = Replace(fString, ""<BR>"", CHR(10))
HTMLDecode = fString
end if
end function
'***********************************************
'repleatBlank函数:自动生成n个空格
'***********************************************
function repleatBlank(n)
n=cint(n)
for u=1 to n
repleatBlank=repleatBlank & "" ""
next
end function
%>