ASP中常用到的函数,给大家分享

<script language="vbscript" runat="server">
'//----------本站公用函数----------------//'
    '*******************************************************
    '函数名:checkVb()
    '作  用:检测数据库操作否正确
    '参  数:无
    '返回值:true    --出错
    '         false   --正确
    '*******************************************************
    Function checkDb()
        If conn.Errors.count > 0 Then
            checkDb=true
        Else
            checkDb=false
        End If
    End Function

    '*******************************************************
    '函数名:clearObj(byVal obj)
    '作  用:销毁对象
    '参  数:obj     --被清理的对象
    '返回值:true    --成功
    '         false   --失败
    '*******************************************************
    Function clearObj(byVal obj)
        If not isObject(obj) then
            clearObj=false
            exit Function
        End If
        obj.close
        set obj=nothing
        clearObj=true
    End Function
   
    '*******************************************************
    '函数名:clearSession(byVal bln)
    '作  用:清理session
    '参  数:bln     --是否执行清理session
    '返回值:true    --成功
    '         false   --失败
    '*******************************************************
    Function clearSession(byVal bln)
        If bln Then session.Abandon
        clearSession=bln
    End Function
   
    '*******************************************************
    '函数名:checkVb()
    '作  用:检测环境否正确
    '参  数:无
    '返回值:true    --出错
    '         false   --正确
    '*******************************************************
    Function checkVb()
        If  Err.number>0 Then
            Err.clear()
            checkVb=true
        Else
            Err.clear()
            checkVb=false
        End If
    End Function
   
    '*******************************************************
    '函数名:stopHere()
    '作  用:停止程序执行
    '参  数:无
    '返回值:无
    '*******************************************************
    Sub stopHere()
        If isObject(rs) Then set rs=nothing
        If isObject(conn) Then set conn=nothing
        response.End()
    End Sub

    '*******************************************************
    '函数名:makeFileWithTime(t, ex)
    '作  用:根据时间生成文件名
    '参  数:t       --时间参数
    '        ex      --扩展名,可以没有
    '返回值:文件名
    '*******************************************************
    Function makeFileWithTime(byVal t, byVal ex)
        If Trim(t)="" or isNull(t) Then
            makeFile=""
            Exit Function
        End If
        t = replace(t,"-","")
        t = replace(t,chr(32),"")
        t = replace(t,":","")
        t = replace(t,"PM","")
        t = replace(t,"AM","")
        t = replace(t,"上午","")
        t = replace(t,"下午","")
        If ex<>"" Then t=t&"."&ex
    End Function

    '*******************************************************
    '函数名:htmlEncode(byVal Str)
    '作  用:保持数据格式不变的函数
    '参  数:Str       --html代码
    '返回值:已经转换的html代码
    '*******************************************************
    Function htmlEncode(byVal Str)
        If Trim(Str) = "" Or IsNull(Str) Then
            htmlEncode = ""
            Exit Function
        End If
            Str = Replace(Str, ">", "&gt;")
            Str = Replace(Str, "<", "&lt;")
            Str = Replace(Str, Chr(32), "&nbsp;")
            Str = Replace(Str ,Chr(9), "&nbsp;")
            Str = Replace(Str, Chr(34), "&quot;")
            Str = Replace(Str, Chr(39), "&#39;")
            Str = Replace(Str, Chr(13), "")
            Str = Replace(Str, Chr(10) & Chr(10), "</p><p>")
               Str = Replace(Str, Chr(10), "<br> ")
            htmlEncode = Str
    End Function
   
    '*******************************************************
    '函数名:Query(byVal ParaName, byVal ParaType)
    '作  用:获取URL,?后面的元素值
    '参  数:ParaName       --参数名称
    '        ParaType       --参数类型(1数字,0字符)
    '返回值:参数不为空返回相应值,并且符合安全;为空返回整个?后面的字串
    '        不符合安全的将返回空值
    '*******************************************************
    Function Query(byVal ParaName, byVal ParaType)
        Dim ParaValue
        If ParaName<>"" Then
            ParaValue=Request.QueryString(ParaName)
            If len(ParaValue)<1 then Exit Function
            If ParaType=0 Then
                ParaValue=replace(ParaValue,"'","''")
            ElseIf ParaType=1 Then
                If Not isNumeric(ParaValue) Then ParaValue=""
            End If
        Else
            ParaValue=replace(Request.QueryString,"'","''")
        End If
        Query=ParaValue
    End Function
   
    '*******************************************************
    '函数名:filtQuery(byVal ParaName)
    '作  用:用来清除QueryString中不需要的hash值
    '参  数:ParaName       --参数名称
    '返回值:新的QueryString字符串,最后包含"&",用来直接添加页码值的
    '*******************************************************
    Function filtQuery(byVal ParaName)
        Dim query
        if len(ParaName) < 1 then exit Function
        For Each query In Request.QueryString
            if LCase(query) <> LCase(ParaName) then filtQuery = filtQuery & query & "=" & Request.QueryString(query) & "&"
            '&用来添加页数的,用不着去掉
        Next
    End Function
    '*******************************************************
    '函数名:out(p)
    '作  用:输入数据到页面,不带任何修饰
    '参  数:p        --输出数据
    '返回值:无
    '*******************************************************
    Sub out(byVal p)
        Response.Write p
    End Sub
   
    '*******************************************************
    '函数名:outLn(p)
    '作  用:输入数据到页面,HTML带换行
    '参  数:p        --输出数据
    '返回值:无
    '*******************************************************
    Sub outLn(byVal p)
        Response.Write p&VBCRLF
    End Sub
   
    '*******************************************************
    '函数名:outBr(byVal p)
    '作  用:输入数据到页面,页面显示带换行
    '参  数:p        --输出数据
    '返回值:无
    '*******************************************************
    Sub outBr(byVal p)
        Response.Write p&"<BR />"&vbCRLF
    End Sub
   
    '*******************************************************
    '函数名:Form(byVal ParaName)
    '作  用:得到提交表单元素的值
    '参  数:ParaName        --表单参数
    '返回值:表单参数不为空,返回相应值;为空返回表单hash值字串
    '*******************************************************
    Function Form(byVal ParaName)
        If ParaName<>"" Then
            Form=trim(request.Form(ParaName))
        Else
            Form=trim(request.Form)
        End If
    End Function

    '*******************************************************
    '函数名:inputSafeDate(str, tp, init)
    '作  用:保证输入数据库中的数据符合类型,安全
    '参  数:str                --得到的数据
    '        type               --数据类型(数字1,字符2,没要求0)
    '        init               --数据为空时替换值
    '返回值:安全的,符合数据库类型的数据
    '*******************************************************
    Function inputSafeDate(byVal str, byVal tp, byVal init)
        str=trim(str)
        outBr str
        If str="" then
            inputSafeDate=init
            Exit Function
        End If
        If tp="" then tp=0
        If tp=0 then   
        ElseIf tp=1 then
            str=cint(str)
        ElseIf tp=2 then
            str=csng(str)
        End If
        inputSafeDate=str
    End Function

    '*******************************************************
    '函数名:urlEncode(str)
    '作  用:对地址进行加密
    '参  数:str --待加密字符串
    '返回值:已经加密了的字符串
    '*******************************************************
    Function urlEncode(byVal str)
        If Trim(str)<>"" or not isNull(str) then
            urlEncode=Server.urlEnCode(str)
        Else
            urlEncode=""
        End If
    End Function
   
    '*******************************************************
    '函数名:isPathFromSite()
    '作  用:判断表单提交路径是否来源本站
    '参  数:无
    '返回值:是 true , 否 false
    '*******************************************************
    Function isPathFromSite()
        dim ServerName1,ServerName2
        ServerName1=Cstr(Request.ServerVariables("HTTP_REFERER"))
        ServerName2=Cstr(Request.ServerVariables("SERVER_NAME"))
        If Mid(ServerName1,8,len(ServerName2))=ServerName2 Then
           isPathFromSite=true
        ELSE
           isPathFromSite=false
        End If
    End Function
   
    '*******************************************************
    '函数名:returnRs(tbl, field, where, order)
    '作  用:得到记录集
    '参  数:tbl                --数据表
    '        field              --提取数据字段
    '        where              --条件
    '        order              --排序字段,倒序
    '返回值:记录集
    '*******************************************************
    Function returnRs(byVal tbl, byVal fld, byVal whr, byVal ord)
        If tbl="" then exit Function
        on error resume next
        If fld="" then fld = "*"
        If whr<>"" then whr=" WHERE "&whr
        If ord<>"" then ord=" ORDER BY "&ord&" DESC"
        dim sql,rs
        If not isObject(conn) Then Exit Function
        set rs = server.createobject("adodb.recordset")
        If ord<>"" then
            sql="SELECT "&fld&" FROM "&tbl&whr&ord
            rs.open sql,conn,1,1
        ElseIf whr<>"" then
            sql="SELECT "&fld&" FROM "&tbl&whr
            rs.open sql,conn,1,3
        Else
            sql="SELECT * FROM "&tbl&" WHERE 1=0"
            rs.open sql,conn,3,3
        End If
        If err then Exit Function
        set returnRs=rs
    End Function
   
    '*******************************************************
    '函数名:getFrontChar(byVal Str, byVal StrLen)
    '作  用:指定最多显示字符数,对多余的进行切取
    '参  数:str                --待处理字符串
    '        strlen             --需要显示的字符个数(半角),-1(全部显示)
    '返回值:处理过的字符串
    '*******************************************************
    Function getFrontChar(byVal Str, byVal StrLen)
        Dim l,t,c,i
        strlen=Clng(strLen)
       
        Dim b'//是否显示全部
        b=true'//默认显示指定字符数
        If strlen=0 and strlen<-1 Then Exit Function
        If strlen=-1 Then b=false
    '--------------------------------------------------------//添加--2007-7-16
   
        If b Then'//判断显示方式
            l=len(str)
            t=0
            for i=1 to l
                c=Abs(Asc(Mid(str,i,1)))
                if c>255 then
                    t=t+2
                else
                    t=t+1
                end if
                if t>=strlen then
                    getFrontChar=left(str,i)&"..."
                    exit for
                else
                getFrontChar=str
                end if
            next
        End If
        getFrontChar = Replace(Replace(Replace(Replace(Replace(getFrontChar, Chr(32), "&nbsp;"), Chr(39), "&#39;"), Chr(34), "&quot;"), ">", "&gt;"), "<", "&lt;")
    end function
</script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值