再发几个ASP不错的函数
编程学习 2021-07-05 10:58www.dzhlxh.cn编程入门
********************
'函数作用:根据条件真假返回选定值中的某个
'参数:blnCondition:条件变量,varResultTrue:条件为真时返回值,varResultFalse:条件为假时返回值
Function IIF(blnCondition, varResultTrue,varResultFalse)
If CBool(blnCondition) Then
IIF = varResultTrue
Else
IIF = varResultFalse
End If
End Function
'********************
'函数作用:判断某个字符串元素是否在给定枚举中
'参数:sEle:待判断的字符串,sArray:指定枚举
'举例:根据扩展名判断是否图片文件:InArray(strFileExt,"jpg,gif,bmp,png")
Function InArray(sEle,sArray)
Dim aArray
Dim i
aArray = Split(sArray,",")
For i = 0 To UBound(aArray)
If Trim(sEle) = Trim(aArray(i)) Then
InArray = True
Exit Function
End If
Next
InArray = False
End Function
'********************
'函数作用:判断某个字符串是否符合正则表达式
'参数:strString:字符串,strPattern:正则表达式
Function doReTest(strString, strPattern)
Dim oRE
Set oRE = New RegExp
oRE.Pattern = strPattern
oRE.IgnoreCase = True
doReTest = oRE.Test(strString)
Set oRE = Nothing
End Function
'********************
'函数作用:正则提取
'参数:string:字符串,patrn:正则表达式
'返回:逗号分割的结果数组集成
Function doReExec(strng,patrn)
Dim regEx, Match, Matches,RetStr ' 创建变量。
Set regEx = New RegExp ' 创建正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置为不区分大小写。
regEx.Global = True ' 设置全局适用。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 对 Matches 集合进行迭代。
RetStr = RetStr & Match.Value & "," & vbCRLF
Next
doReExec = RetStr
End Function
复制代码 '********************
'函数作用:显示分页链接
'参数:lngCurPage:当前页是第几页,lngPageCount:一共几页,strSueryString:分页链接需要附加的QueryString变量
Sub showPageNav(lngCurPage,lngPageCount,ByVal strQueryString)
Response.Write "当前第" & lngCurPage & "页,共:" & lngPageCount & "页"
Dim i,j,k
If lngCurPage = 1 Then '如果是第一页
'如果lngPageCount小于10,则导航页最多到lngPageCount页
If lngPageCount < 10 Then
j = lngPageCount
Else
j = 10
End If
For i = 2 To j
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
ElseIf lngCurPage = lngPageCount Then '如果是最后一页
'如果lngPageCount小于10,则导航起始从1开始
If lngPageCount < 10 Then
j = 1
Else
j = lngPageCount - 10
End If
For i = j To lngPageCount - 1
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(lPageCount)
Else '如果是中间的页
If lngCurPage <= 5 Then
j = 1
Else
j = lngCurPage - 5
End If
If lngPageCount <= lngCurPage + 5 Then
k = lngPageCount
Else
k = lngCurPage + 5
End If
Response.Write("<a href=""?" & strQueryString & "&p=" & 1 & """>" & "<<" & "</a> ")
For i = j To lngCurPage - 1
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(lngCurPage & " ")
For i = lngCurPage + 1 To k
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(" <a href=""?" & strQueryString & "&p=" & lPageCount & """>" & ">>" & "</a>")
End If
End Sub
'********************
'函数作用:当前页请求方式是否为POST
'说明:用于在同一页面处理显示和数据操作,当PostBack()为真时说明提交表单至当前页,应进行数据后台操作
Function PostBack()
If UCase(Trim(Request.ServerVariables("REQUEST_METHOD"))) = "POST" Then
PostBack = True
Else
PostBack = False
End If
End Function
'********************
'函数作用:返回执行长度的随机字符串
'参数:Length:长度
Function GenRadomString(Length)
dim i, tempS, v
dim c(39)
tempS = ""
c(1) = "a": c(2) = "b": c(3) = "c": c(4) = "d": c(5) = "e": c(6) = "f": c(7) = "g"
c(8) = "h": c(9) = "i": c(10) = "j": c(11) = "k": c(12) = "l": c(13) = "m": c(14) = "n"
c(15) = "o": c(16) = "p": c(17) = "q": c(18) = "r": c(19) = "s": c(20) = "t": c(21) = "u"
c(22) = "v": c(23) = "w": c(24) = "x": c(25) = "y": c(26) = "z": c(27) = "1": c(28) = "2"
c(29) = "3": c(30) = "4": c(31) = "5": c(32) = "6": c(33) = "7": c(34) = "8": c(35) = "9"
If isNumeric(Length) = False Then
Response.Write "A numeric datatype was not submitted to this function."
Exit Function
End If
For i = 1 to Length
Randomize
v = Int((35 * Rnd) + 1)
tempS = tempS & c(v)
Next
GenRadomString = tempS
End Function
'函数作用:根据条件真假返回选定值中的某个
'参数:blnCondition:条件变量,varResultTrue:条件为真时返回值,varResultFalse:条件为假时返回值
Function IIF(blnCondition, varResultTrue,varResultFalse)
If CBool(blnCondition) Then
IIF = varResultTrue
Else
IIF = varResultFalse
End If
End Function
'********************
'函数作用:判断某个字符串元素是否在给定枚举中
'参数:sEle:待判断的字符串,sArray:指定枚举
'举例:根据扩展名判断是否图片文件:InArray(strFileExt,"jpg,gif,bmp,png")
Function InArray(sEle,sArray)
Dim aArray
Dim i
aArray = Split(sArray,",")
For i = 0 To UBound(aArray)
If Trim(sEle) = Trim(aArray(i)) Then
InArray = True
Exit Function
End If
Next
InArray = False
End Function
'********************
'函数作用:判断某个字符串是否符合正则表达式
'参数:strString:字符串,strPattern:正则表达式
Function doReTest(strString, strPattern)
Dim oRE
Set oRE = New RegExp
oRE.Pattern = strPattern
oRE.IgnoreCase = True
doReTest = oRE.Test(strString)
Set oRE = Nothing
End Function
'********************
'函数作用:正则提取
'参数:string:字符串,patrn:正则表达式
'返回:逗号分割的结果数组集成
Function doReExec(strng,patrn)
Dim regEx, Match, Matches,RetStr ' 创建变量。
Set regEx = New RegExp ' 创建正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置为不区分大小写。
regEx.Global = True ' 设置全局适用。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 对 Matches 集合进行迭代。
RetStr = RetStr & Match.Value & "," & vbCRLF
Next
doReExec = RetStr
End Function
复制代码 '********************
'函数作用:显示分页链接
'参数:lngCurPage:当前页是第几页,lngPageCount:一共几页,strSueryString:分页链接需要附加的QueryString变量
Sub showPageNav(lngCurPage,lngPageCount,ByVal strQueryString)
Response.Write "当前第" & lngCurPage & "页,共:" & lngPageCount & "页"
Dim i,j,k
If lngCurPage = 1 Then '如果是第一页
'如果lngPageCount小于10,则导航页最多到lngPageCount页
If lngPageCount < 10 Then
j = lngPageCount
Else
j = 10
End If
For i = 2 To j
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
ElseIf lngCurPage = lngPageCount Then '如果是最后一页
'如果lngPageCount小于10,则导航起始从1开始
If lngPageCount < 10 Then
j = 1
Else
j = lngPageCount - 10
End If
For i = j To lngPageCount - 1
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(lPageCount)
Else '如果是中间的页
If lngCurPage <= 5 Then
j = 1
Else
j = lngCurPage - 5
End If
If lngPageCount <= lngCurPage + 5 Then
k = lngPageCount
Else
k = lngCurPage + 5
End If
Response.Write("<a href=""?" & strQueryString & "&p=" & 1 & """>" & "<<" & "</a> ")
For i = j To lngCurPage - 1
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(lngCurPage & " ")
For i = lngCurPage + 1 To k
Response.Write("<a href=""?" & strQueryString & "&p=" & i & """>" & i & "</a> ")
Next
Response.Write(" <a href=""?" & strQueryString & "&p=" & lPageCount & """>" & ">>" & "</a>")
End If
End Sub
'********************
'函数作用:当前页请求方式是否为POST
'说明:用于在同一页面处理显示和数据操作,当PostBack()为真时说明提交表单至当前页,应进行数据后台操作
Function PostBack()
If UCase(Trim(Request.ServerVariables("REQUEST_METHOD"))) = "POST" Then
PostBack = True
Else
PostBack = False
End If
End Function
'********************
'函数作用:返回执行长度的随机字符串
'参数:Length:长度
Function GenRadomString(Length)
dim i, tempS, v
dim c(39)
tempS = ""
c(1) = "a": c(2) = "b": c(3) = "c": c(4) = "d": c(5) = "e": c(6) = "f": c(7) = "g"
c(8) = "h": c(9) = "i": c(10) = "j": c(11) = "k": c(12) = "l": c(13) = "m": c(14) = "n"
c(15) = "o": c(16) = "p": c(17) = "q": c(18) = "r": c(19) = "s": c(20) = "t": c(21) = "u"
c(22) = "v": c(23) = "w": c(24) = "x": c(25) = "y": c(26) = "z": c(27) = "1": c(28) = "2"
c(29) = "3": c(30) = "4": c(31) = "5": c(32) = "6": c(33) = "7": c(34) = "8": c(35) = "9"
If isNumeric(Length) = False Then
Response.Write "A numeric datatype was not submitted to this function."
Exit Function
End If
For i = 1 to Length
Randomize
v = Int((35 * Rnd) + 1)
tempS = tempS & c(v)
Next
GenRadomString = tempS
End Function