asp.net一个公共函数文件
发布时间:2016-07-04 11:36:32编辑:丝画阁阅读(582)
//获取URL地址.
function GetHttpPage(url)
dim adxmlhttp
'set adxmlhttp = Server.createobject("microsoft.xmlhttp")
set adxmlhttp=server.createobject("MSXML2.serverXMLHTTP")
adxmlhttp.SetTimeOuts 20000, 20000, 30000, 30000
adxmlhttp.open "get",url,false
adxmlhttp.send()
if adxmlhttp.readystate <> 4 then exit function
GetHttpPage = Bytes2bStr(adxmlhttp.responsebody)
set adxmlhttp = nothing
End function
//字符串编码转换
function Bytes2bStr(vin)
Dim BytesStream,StringReturn
Set BytesStream = Server.CreateObject("adodb.stream")
BytesStream.Type = 2
BytesStream.Open
BytesStream.WriteText vin
BytesStream.Position = 0
BytesStream.Charset = "GB2312"
BytesStream.Position = 2
StringReturn =BytesStream.ReadText
BytesStream.close
Set BytesStream = Nothing
Bytes2bStr = StringReturn
End function
//字符串截取
Function strCut(strContent,StartStr,EndStr,CutType)
Dim strHtml,S1,S2
strHtml = strContent
On Error Resume Next
If CutType=2 Then
S1 = InStr(strHtml,StartStr)+Len(StartStr)
S2 = InStr(S1,strHtml,EndStr)
If Err Then
response.write "Unknow Wrong:"&err.description&"---BG:" & S1 & " End:"&S2&"<br>"
Err.Clear
strCut=""
Exit Function
Else
If S1>Len(StartStr) and S2>0 then
strCut=Mid(strHtml,S1,S2-S1)
Else
strCut=""
End If
End If
Else
S1 = InStr(strHtml,StartStr)
S2 = InStr(S1,strHtml,EndStr)+Len(EndStr)
If Err Then
response.write "Unknow Wrong:"&err.description&"---BG:" & S1 & " End:"&S2&"<br>"
Err.Clear
strCut=""
Exit Function
Else
If S1>0 and S2>Len(EndStr) then
strCut=Mid(strHtml,S1,S2-S1)
Else
strCut=""
End If
End if
End If
End Function
//正则替换字符串
Function ReplaceReg(str,patrn,replStr,Ignor)
Dim regEx
If Ingor=1 Then Ingor=true else Ingor=false
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = Ignor
regEx.Global=True
ReplaceReg = regEx.Replace(str,replStr)
End Function
//时间转换
Function DateStringFromNow(sTheDate)
' 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
Dim iSeconds, iMinutes, iHours, iDays
iSeconds = DateDiff("s", sTheDate, Now()) 'd/h/n/s
iMinutes = Int(iSeconds/60)
iHours = Int(iSeconds/3600)
iDays = Int(iSeconds/86400)
If iDays > 60 Then
DateStringFromNow = CInt(iDays/30)&"个月前"
ElseIf iDays > 1460 Then
DateStringFromNow = "3年前"
ElseIf iDays > 730 Then
DateStringFromNow = "2年前"
ElseIf iDays > 365 Then
DateStringFromNow = "1年前"
Elseif (iSeconds >= 2592000 and iSeconds < 31104000) Then
DateStringFromNow = INT(iSeconds/2592000)&"个月前"
ElseIf iDays > 30 Then
DateStringFromNow = "1个月前"
ElseIf iDays > 21 Then
DateStringFromNow = "3周前"
ElseIf iDays > 14 Then
DateStringFromNow = "2周前"
ElseIf iDays > 7 Then
DateStringFromNow = "1周前"
ElseIf iDays > 1 Then
DateStringFromNow = iDays & "天前"
ElseIf iHours > 1 Then
DateStringFromNow = iHours & "小时前"
ElseIf iMinutes > 1 Then
DateStringFromNow = iMinutes & "分钟前"
ElseIf iSeconds >= 1 Then
DateStringFromNow = iSeconds & "秒前"
Else
DateStringFromNow = "1秒前"
End If
End Function
//用户名添加星号
Function FormatUN(UserNameString)
If Len(UserNameString)=11 Then
UserNameString=Left(UserNameString,6)
FormatUN=UserNameString&"*****"
Else
FormatUN=UserNameString
End if
End Function
//发送短信
Function getSmsCode(content,mobile)
codeXml=GetHttpPage("http://106.ihuyi.cn/webservice/sms.php?method=Submit&account=cf_renhai&password=19750203&mobile="&mobile&"&content="&content)
len1 = instr(1,codeXml,"</code>",1)-1
len2 = instr(1,codeXml,"<code>",1)
code=left(codeXml,len1)
code=right(code,(len1-len2-5))
getSmsCode=code
End Function
//字符转换
function HTMLEncode(fString)
fString = replace(fString, "&", "&")
fString = replace(fString, """", """)
fString = replace(fString, "'", "'")
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = replace(fString, " ", " ")
fString = replace(fString, CHR(13), "")
fString = replace(fString, CHR(10) & CHR(10), "<p></p>")
fString = replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
//随机省份
Function getRandomProvince()
Randomize
province=Array("北京","天津","山西","河北","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆")
getRandomProvince=province(Int(30*Rnd))
End Function
//随机获取一个姓氏
Function getRandomFirstName()
Randomize
names=Array("赵","钱","孙","李","周","吴","郑","王","冯","陈","蒋","沈","韩","杨","朱","秦","许","何","吕","张","曹","严","金","魏","陶","姜","谢","潘","葛","范","彭","鲁","马","任","袁","史","唐","殷","罗","郝","安","于","余","黄","汪","宋","庞","纪","董","梁","杜","贾","江","郭","林","徐","高","夏","蔡","田","樊","胡","万","卢","丁","邓","崔","程","邢","裴","陆","焦","谷","伊","武","刘","叶","白","乔","翟","申","牛","耿","巩","聂","辛","关")
getRandomFirstName=names(Int((ubound(names))*Rnd))
End Function
//获取随机手机号
Function getRandomMobile()
Randomize
m_1=Array(3,5,8)
m_2=Array(0,1,2,3,4,5,6,7,8,9)
getRandomMobile="1"&m_1(Int((ubound(m_1)+1)*Rnd))&m_2(Int((ubound(m_2)+1)*Rnd))&"****"&Int(8999 * Rnd + 1000)
End Function
//获取随机商品
Function getRandomProduct(s_ProInfo)
Randomize
pro_arry=Split(s_ProInfo,chr(10))
product=Split(pro_arry(Int(ubound(pro_arry))*Rnd),"|")
getRandomProduct=product(0)
End Function