我常用的一些ASP自訂函數

來源:互聯網
上載者:User

  以下這些函數是我這麼多年來的一個總結,有些是在工作中不斷總結出來的,有些是通過網路的一些常式收集回來的,希望這些能對大家有協助,多謝!

'自訂MyFormatDateTime函數的FormatType參數項目
Const fdDefaultDateTime=1 '格式為:yyyy-m-d h:m:s
Const fdDefaultDate=2     '格式為:yyyy-m-d
Const fdDefaultTime=3     '格式為:h:m:s
Const fdChineseDate=4 '格式為:yyyy年m月d日
Const fdLongDateTime=5 '格式為:yyyy-mm-dd hh:mm:ss
Const fdLongDate=6 '格式為:yyyy-mm-dd
Const fdLongTime=7 '格式為:hh:mm:ss

'自訂擷取QueryString、Form、Cookies、ServerVariables值GetRequestValue函數的mode參數
Const gvALL=1 '等於:request("key")
Const gvForm=2 '等於:request.form("key")
Const gvQuery=3 '等於:request.QueryString("key")
Const gvCookies=4 '等於:request.Cookies("key")
Const gvIP=5 '等於:Request.ServerVariables("REMOTE_ADDR")
Const gvURL=6 '等於:Request.ServerVariables("URL")

'自訂函數CheckInput函數DataType參數
Const ciNumber=1
Const ciString=2
Const ciDateTime=3

'*********************************************************
' 目的: 對日期輸出進行格式化
'         
' 輸入: data:要輸出的數字
'       FormatType:日期格式
'     Const fdDefaultDateTime=1 '格式為:yyyy-m-d h:m:s
'     Const fdDefaultDate=2     '格式為:yyyy-m-d
'     Const fdDefaultTime=3     '格式為:h:m:s
'     Const fdChineseDate=4 '格式為:yyyy年m月d日
'     Const fdLongDateTime=5 '格式為:yyyy-mm-dd hh:mm:ss
'     Const fdLongDate=6 '格式為:yyyy-mm-dd
'     Const fdLongTime=7 '格式為:hh:mm:ss
'
' 傳回值: 格式化後的日期文字
'        
'*********************************************************
Function MyFormatDateTime(data,FormatType)
 if not(isdate(data)) then data=date
 select case FormatType
  case fdDefaultDateTime
   data=year(data)&"-"&month(data)&"-"&day(data)&" "&hour(data)&":"&minute(data)&":"&second(data)
  case fdDefaultDate
   data=year(data)&"-"&month(data)&"-"&day(data)
  case fdDefaultTime
   data=hour(data)&":"&minute(data)&":"&second(data)
  case fdChineseDate
   data=year(data)&"年"&month(data)&"月"&day(data)&"日"
  case fdLongDateTime
   data=year(data)&"-"&MyFormatNumber(month(data),2)&"-"&MyFormatNumber(day(data),2)&" "&MyFormatNumber(hour(data),2)&":"&MyFormatNumber(minute(data),2)&":"&MyFormatNumber(second(data),2)
  case fdLongDate
   data=year(data)&"-"&MyFormatNumber(month(data),2)&"-"&MyFormatNumber(day(data),2)
  case fdLongTime
   data=MyFormatNumber(hour(data),2)&":"&MyFormatNumber(minute(data),2)&":"&MyFormatNumber(second(data),2)
 end Select
 MyFormatDateTime=data
End Function

'*********************************************************
' 目的: 對數字輸出進行格式化
'         
' 輸入: data:要輸出的數字
'       NumLength:要輸出的數字長度
'       例如:data=1  NumLength=3  返回 001
'             data=11  NumLength=5  返回 00011
'
' 傳回值: 格式化後的數字字串
'        
'*********************************************************
Function MyFormatNumber(data,NumLength)
 dim FillLength
 Filllength=NumLength-len(data)
 MyFormatNumber=String(Filllength,"0") & data
End Function

'*********************************************************
' 目的: 對輸出字串進行HTML編碼
'         
' 輸入: p_STR:要輸出的字元
'
' 傳回值: 替換後的字串
'        
'*********************************************************
Function HTMLEncode(p_STR)
 if not isnull(p_STR) then
     p_STR = replace(p_STR, ">", ">")
     p_STR = replace(p_STR, "<", "&lt;")
     p_STR = Replace(p_STR, CHR(32), "&nbsp;")
     p_STR = Replace(p_STR, CHR(9), "&nbsp;")
     p_STR = Replace(p_STR, CHR(34), "&quot;")
     p_STR = Replace(p_STR, CHR(39), "&#39;")
     p_STR = Replace(p_STR, CHR(13) , "<BR>")
    HTMLEncode = p_STR
 End if
End Function

'*********************************************************
' 目的: 對輸出字串進行UBB替換
'         
' 輸入: p_STR:要輸出的字串
'
' 傳回值: 替換後的字串
'        
'*********************************************************
function UBBCode(p_STR)
 p_STR = HTMLEncode(p_STR)
  dim re
  Set re=new RegExp
  re.IgnoreCase =true
  re.Global=True

  re.Pattern="(?:/[IMG/])(.[^/[]*)(?:/[//IMG/])"
  strContent=re.Replace(p_STR,"<div width='100%' align='center'><img  width='460' src=""$1"" border=""0""></div>")
  re.Pattern="(?:/[B/])(.[^/[]*)(?:/[//B/])"
  strContent=re.Replace(p_STR,"<b>$1</b>")
  re.Pattern="(?:/[URL=(.[^/[]*)/])(.[^/[]*)(?:/[//URL/])"
  strContent=re.Replace(p_STR,"<a href=""$1"" target=""_blank"">$2</a>")
  set re=Nothing
  UBBCode=p_STR
end function

'*********************************************************
' 目的: 替代response.write p_Str
'         
' 輸入: p_STR:要輸出的字串
'
' 傳回值: 無
'        
'*********************************************************
Function echo(p_STR)
    response.write p_Str
end function

'*********************************************************
' 目的: 替代response.write p_Str & "<br>"
'           response.end
'         
' 輸入: p_STR:要輸出的字串
'
' 傳回值: 無
'        
'*********************************************************
Function die(p_STR)
    echo p_Str
    response.end
end function

'*********************************************************
' 目的: 替代response.write p_Str & "<br>"
'         
' 輸入: p_STR:要輸出的字串
'
' 傳回值: 無
'        
'*********************************************************
Function echobr(p_STR)
    echo p_Str & "<br>" & vbCRLF
end function

'*********************************************************
' 目的: 替代request.cookies("key")=p_STR
'         
' 輸入: key: 輸出的關鍵字
'       p_STR:要輸出的字串
'       pExpires:cookies存在時間
'
' 傳回值: 無
'        
'*********************************************************
Function echock(key,p_STR,pExpires)
 response.cookies(key)=p_STR
 if not (isnull(pExpires) or pExpires="") then response.cookies(key).Expires=pExpires
End Function

'*********************************************************
' 目的: 替代Request的取值
'         
' 輸入: key: 取值的關鍵字
'       pMode:取值方式
'     Const gvALL=1 '等於:request("key")
'     Const gvForm=2 '等於:request.form("key")
'     Const gvQuery=3 '等於:request.QueryString("key")
'     Const gvCookies=4 '等於:request.Cookies("key")
'     Const gvIP=5 '等於:Request.ServerVariables("REMOTE_ADDR")
'     Const gvURL=6 '等於:Request.ServerVariables("URL")
'
' 傳回值: 返回request後的值
'        
'*********************************************************
Function GetRequestValue(key,pMode)
 dim p_STR
 Select Case pMode
  Case gvALL
   p_STR=request(key)
  Case gvForm
   p_STR=request.form(key)
  Case gvQuery
   p_STR=request.querystring(key)
  Case gvCookies
   p_STR=request.cookies(key)
  Case gvIP
   P_STR=Request.ServerVariables("REMOTE_ADDR")
  Case gvURL
   p_STR=Request.ServerVariables("URL")
 End Select
 GetRequestValue=p_STR
End Function

'*********************************************************
' 目的: 對使用者提交的資料進行類型檢查
'         
' 輸入: exp1: 要檢查的資料
'       DataType:要檢查的資料類型。
'     ciNumber:數字 
'     ciString:字元
'         ciDateTime:日期
'       DefaultValue:預設值。
' 傳回值: 如果資料通過檢查則返回原值,字元類型需要替換單引號。
'         如果不符合則返回預設值
'*********************************************************
Function CheckInput(exp1,DataType,DefaultValue)
 dim exp2
 if isnull(exp1) or exp1="" then
  exp2=DefaultValue
 else
  Select Case DataType
  case ciString
   exp2=Replace(exp1,"'","''")
  case ciNumber
   if isNumeric(exp1) then
    exp2=exp1
   else
    exp2=DefaultValue
   end if
  case ciDateTime
   if isdate(exp1) then
    exp2=exp1
   else
    exp2=DefaultValue
   end if
  end select
 end if
 CheckInput=exp2
End Function

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.