본문 바로가기

DeveloP/ASP

ASP 함수 라이브러리


<%
' --------------------------------------------------------
' 문자열을 원하는 길이만큼 자르기
' --------------------------------------------------------
function cutstr(long_str, cutting_len)
 dim i, short_str, tail
 
 tail = "..."
 long_str = trim(long_str)

 for i = 0 to cutting_len-1
  if asc(mid(long_str, i+1, 1)) >= 128 then
   kor = kor + 1
  else
   eng = eng + 1
  end if
 next
 if len(long_str) > cutting_len then
  if kor mod 2 = 1 then
   cutting_len = cutting_len - 1
  end if
  short_str = mid(long_str, 1, cutting_len)
  cutstr = short_str & tail
 else
  short_str = mid(long_str, 1, cutting_len)
  cutstr = short_str
 end if
end function


' --------------------------------------------------------
' 정규표현식을 이용한 대치
' --------------------------------------------------------    
Function ereg_replace(pattern, strReplace, text)
 Dim eregObj
  
 ' Create regular expression
 Set eregObj= New RegExp
   
 eregObj.Pattern= pattern ' Set Pattern(패턴 설정)
 eregObj.IgnoreCase = False ' Set Case Insensitivity(대소문자 구분 여부)
 eregObj.Global = True  ' Set All Replace(전체 문서에서 검색)
   
 ereg_replace = eregObj.Replace(text, strReplace) ' Replace String 
End Function

Function eregi_replace(pattern, strReplace, text)
 Dim eregObj
  
 ' Create regular expression
 Set eregObj= New RegExp
   
 eregObj.Pattern= pattern ' Set Pattern(패턴 설정)
 eregObj.IgnoreCase = True ' Set Case Insensitivity(대소문자 구분 여부)
 eregObj.Global = True  ' Set All Replace(전체 문서에서 검색)
   
 eregi_replace = eregObj.Replace(text, strReplace) ' Replace String 
End Function 

Function ereg(patrn, strng)
 Dim regEx, Match, Matches, i, regs()   ' Create variable.
 
 Set regEx = New RegExp   ' Create a regular expression.
 regEx.Pattern = patrn   ' Set pattern.
 regEx.IgnoreCase = False   ' Set case insensitivity.
 regEx.Global = True   ' Set global applicability.
 
 Set Matches = regEx.Execute(strng)   ' Execute search.
 
 regs(0) = strng
 i = 1
 For Each Match in Matches   ' Iterate Matches collection.
  regs(i) = Match.Value
  i = i+1
 Next
 ereg = regs
End Function

Function eregi(patrn, strng)
 Dim regEx, Match, Matches, i, regs()   ' Create variable.
 
 Set regEx = New RegExp   ' Create a regular expression.
 regEx.Pattern = patrn   ' Set pattern.
 regEx.IgnoreCase = True   ' Set case insensitivity.
 regEx.Global = True   ' Set global applicability.
 
 Set Matches = regEx.Execute(strng)   ' Execute search.
 
 i = 1
 regs(0) = strng
 For Each Match in Matches   ' Iterate Matches collection.
  regs(i) = Match.Value
  i = i+1
 Next
 eregi = regs
End Function

' --------------------------------------------------------
' Tag를 삭제한다.
' --------------------------------------------------------
Function strip_tag(atcText)
 atcText= eregi_replace("&lt;", "<", atcText)
 atcText= eregi_replace("&gt;", ">", atcText)
 atcText= eregi_replace("<html(.*|)<body([^>]*)>","",atcText)
 atcText= eregi_replace("</body(.*)</html>(.*)","",atcText)
 atcText= eregi_replace("<[/]*(div|layer|body|html|head|meta|form|input|select|textarea|base)[^>]*>","",atcText)
 atcText= eregi_replace("<(style|script|title|link)(.*)</(style|script|title)>","",atcText)
 atcText= eregi_replace("<[/]*(script|style|title|xmp)>","",atcText)
 atcText= eregi_replace("([a-z0-9]*script:)","deny_$1",atcText)
 atcText= eregi_replace("<(\?|%)","&lt;$1",atcText)
 atcText= eregi_replace("(\?|%)>","$1&gt;",atcText)
 
 ' Tag가 제거된 문자열을 리턴한다.
 strip_tag = atcText
End Function

' --------------------------------------------------------
' 문서 내용에 있는 Url를 찾아내어  자동으로 링크를 구성하는 함수
' --------------------------------------------------------  
Function auto_link(text)
 Dim regex_file, regex_http, regex_mail
   
 regex_file= "gz|tgz|tar|gzip|zip|rar|mpeg|mpg|exe|rpm|dep|rm|ram|asf|ace|viv|avi|mid|gif|jpg|png|bmp|eps|mov"
 regex_http = "(http|https|ftp|telnet|news):\/\/(([\xA1-\xFEa-z0-9_\-]+\.[][\xA1-\xFEa-z0-9:;&#@=_~%\?\/\.\,\+\-]+)(\/|[\.]*[a-z0-9]))"
 regex_mail = "([\xA1-\xFEa-z0-9_\.\-]+)@([\xA1-\xFEa-z0-9_\-]+\.[a-z0-9\-\._\-]+[\.]*[\xA1-\xFEa-z0-9\?=]*)"
   
 ' img tag 와 a tag 의 경우 링크가 여러줄에 걸쳐 이루어져 있을 경우
 ' 이를 한줄로 합침 (합치면서 부가 옵션들은 모두 삭제함)
 text = eregi_replace("<(a|img)[^>]*(href|src)[^>]*(" & regex_http & "|mailto:" & regex_mail & ")[^>]*>","<$1 $2=""$3"">", text)
   
 ' 특수문자와 링크시 target 삭제
 text = eregi_replace("&(quot|gt|lt)","!$1", text)
 
 ' html 사용시 Link 보호
 text  = eregi_replace("href=""(" & regex_http & ")""[^>]*>","href=""$2_orig://$3"" target=""_blank"">", text)
 text  = eregi_replace("href=""mailto:(" & regex_mail & ")"">","href=""mailto:$2#-#$3"">", text) 
 text  = eregi_replace("(background|codebase|src)[ \n]*=[\n""' ]*(" & regex_http & ")[""']*","$1=""$3_orig://$4""",text)
 
 '링크가 안된 Url및 Email Address 자동 링크
 text  = eregi_replace("(" & regex_http & ")" ,"<a href=""$1"" target=""_blank"">$1</a>", text)
 text  = eregi_replace("(" & regex_mail & ")","<a href=""mailto:$1"">$1</a>", text)
 ' 보호를 위해 치환된것 복구
 text = eregi_replace("!(quot|gt|lt)","&$1", text)
 text = eregi_replace("(http|https|ftp|telnet|news|mms)_orig","$1", text)
 text = eregi_replace("#-#","@",text)
 ' File Link시 Target을 삭제  
 text = eregi_replace("(\.(" & regex_file & ")"") target=""_blank""","$1", text)
 
 auto_link = text
End Function

' --------------------------------------------------------
' Quick Sort
' --------------------------------------------------------
Function QuickSort(vec, loBound, hiBound)
    Dim pivot,loSwap,hiSwap,temp

 ' QuickSort는 정렬이 안된 배열을 정렬할 때 가장 빠른 방식이다.

    ' 정렬되어야 할 아이템이 두 개일 경우
    if hiBound - loBound = 1 then
        if vec(loBound) > vec(hiBound) then
            temp=vec(loBound)
            vec(loBound) = vec(hiBound)
            vec(hiBound) = temp
        End If
    End If

    ' 정렬되어야 할 아이템이 세 개 이상일 경우
    pivot = vec(int((loBound + hiBound) / 2))
    vec(int((loBound + hiBound) / 2)) = vec(loBound)
    vec(loBound) = pivot
    loSwap = loBound + 1
    hiSwap = hiBound

    do
        ' 적절한 loSwap 찾기
        while loSwap < hiSwap and vec(loSwap) <= pivot
            loSwap = loSwap + 1
        wend
        ' 적절한 hiSwap 찾기
        while vec(hiSwap) > pivot
            hiSwap = hiSwap - 1
        wend
        ' loSwap이 hiSwap보다 작으면 값 교체
        if loSwap < hiSwap then
            temp = vec(loSwap)
            vec(loSwap) = vec(hiSwap)
            vec(hiSwap) = temp
        End If
    loop while loSwap < hiSwap

    vec(loBound) = vec(hiSwap)
    vec(hiSwap) = pivot
    ' QuickSort의 백미 : 재귀 호출(Recursively call function)
    '첫 번째 섹션에서 두 개 또는 그 이상의 아이템
    if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1)
    '두 번째 섹션에서 두 개 또는 그 이상의 아이템
    if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound)
End Function

' ----------------------------------------------------------
' 페이지번호나 레코드번호 값을 받아 페이지리스트 출력
' ----------------------------------------------------------
' pi 배열의 상수
Const piTotal_page = 0, piStart_rec = 1, piEnd_rec = 2, piStart_page = 3
Const piEnd_page = 4, piPre_page = 5, piNext_page = 6, piCurrent_page = 7

function page_list(t_rec, rows, pages, rec_num)
 page = int(request("page"))
 if page < 1 then page = 1
 ' 게시물에 해당하는 페이지구함
 if rec_num > -1 then page = ceil((rec_num+1) / rows)

 ' 총페이지 수
 t_page = ceil(t_rec / rows)
 
 ' 레코드의 시작과 끝
 s_rec = rows * (page-1)
 if (t_rec - (s_rec+rows)) < 0 then
  e_rec = t_rec
 else
  e_rec = s_rec+rows
 end if
 
 ' 페이지의 시작과 끝
 s_page = int((page-1) / pages) * pages
 if t_page - (s_page+pages) < 0 then
  e_page = t_page
 else
  e_page = s_page+pages
 end if
 
 ' 이전, 다음 페이지 블럭번호
 if s_page > 0 then
  p_page = s_page
 else
  p_page = 1
 end if
 
 if t_page > e_page then
  n_page = e_page+1
 else
  n_page = t_page
 end if
 
 ' 총페이지, 시작레코드, 마지막레코드, 시작페이지, 마지막페이지, 이전페이지, 다음페이지
 pi = array(t_page, s_rec, e_rec, s_page+1, e_page, p_page, n_page, page)
 
 page_list = pi
end function

' ----------------------------------------------------------
' page번호 출력
' ----------------------------------------------------------
function page_print(pi, page_per_block, get_param)
 ' 처음, 이전 출력
 if pi(PiStart_page) > 1 then
  pp = "<a href=" & get_param & "&page=1>[처음] </a>" & _
   "<a href=" & get_param & "&page=" & pi(PiPre_page) & ">[이전 " & _
   page_per_block & "개]</a> "
 end if
 ' 페이지 번호 출력
 for i = pi(piStart_page) to pi(piEnd_page)
  if i = pi(piCurrent_page) then
   pp = pp & "<font color='red'>[" & i & "]</font> "
  else
   pp = pp & "<a href=" & get_param & "&page=" & i & ">[" & i & "]</a> "
  end if
 next
 ' 다음, 끝 출력
 if pi(piEnd_page) < pi(piTotal_page) then
  pp = pp & "<a href=" & get_param & "&page=" & pi(piNext_page) & ">[다음 " & page_per_block & "개]</a> " &_
   "<a href=" & get_param & "&page=" & pi(piTotal_page) & ">[끝]</a> "
 end if
 
 page_print =  pp
end function

' ----------------------------------------------------------
' nl2br() 구현
' ----------------------------------------------------------
function nl2br(str)
 nl2br = ereg_replace("(\r\n|\n\r|\r|\n)", "<br>", str)
end function

' ----------------------------------------------------------
' table tag안에 br을 제거한다.
' ----------------------------------------------------------
function conv_nl(str)
 'str = stripslashes($str);
 str = eregi_replace("<br>(\r\n|\n\r|\r|\n)", vbLf, str)
 str = ereg_replace("(\r\n|\n\r|\r)", vbLf, str)

 ' table안에 \n이 br로 치환되지 않도록 구분함.
 str = eregi_replace("<(/?(TABLE|TR|TH|TD)[^>]*)> *\n", "<$1>\t_nl_\t", str)
 ' table태그 앞에 공백을 없앤다.
 str = eregi_replace(" *<(/?(TABLE|TR|TH|TD)+)", "<$1", str)
 ' <, > 태그 안에 \n 제거
 str = ereg_replace("<([^><\n]*)\n+([^><\n]*)>", "<$1 $2>", str)
 
 str = ereg_replace("\n[:space:]*\n", "<BR>" & vbLf, str)
 str = ereg_replace("\n\n", "<BR><BR>" & vbLf, str)
 
 str = nl2br(str)
 str = ereg_replace("\t_nl_\t", vbLf, str)
 str = eregi_replace("</?pre>", "", str)
 
 conv_nl =  str
end function

' ----------------------------------------------------------
' php의 ceil() 과 같은 기능을 한다.
' ----------------------------------------------------------
function ceil(num)
 ' 소수점이 있으면 1을 더한다.
 if num - int(num) > 0 then
  ceil = Int(num) + 1
 else
  ceil = Int(num)
 end if
end function

' --------------------------------------------
' 암호화
' --------------------------------------------
function Encrypt(theText)
 Dim output, Temp(), Temp2()
 
 TextSize = Len(theText)
 if TextSize < 1 then
  Encrypt = ""
  exit function
 end if
 ReDim Temp(TextSize-1)
 ReDim Temp2(TextSize-1)

 Randomize
 for i = 0 to TextSize-1
  rndVal = Int((255 * Rnd) + 128)
  ' mid 는 자동으로 2바이트 문자를 1글자로 인식한다.
  Temp(i) = AscW(Mid(theText, i+1, 1)) + rndVal
  Temp2(i) = rndVal
 next
 for i = 0 to TextSize-1
  output = output & ChrW(Temp(i)) & ChrW(Temp2(i))
 next
 
 'Encrypt = Escape(output)
 Encrypt = output
end function

' --------------------------------------------
' 복호화
' --------------------------------------------
function unEncrypt(theText)
 Dim output, Temp(), Temp2()
 
 'theText = unEscape(theText)
 TextSize = Len(theText)
 if TextSize < 1 then
  unEncrypt = ""
  exit function
 end if
 ReDim Temp(TextSize-1)
 ReDim Temp2(TextSize-1)
 
 for i = 0 to TextSize-2 step 2
  Temp(i) = AscW(Mid(theText, i+1, 1))
  Temp2(i) = AscW(Mid(theText, i+2, 1))
 next
 for i = 0 to TextSize-1 step 2
  output = output & ChrW(Temp(i) - Temp2(i))
 next
 
 unEncrypt = output
end function

' ---------------------------------------------------
' 이미지를 불러와 가로, 세로크기를 구한다.
' ---------------------------------------------------
function getImageSize(imgPath)
 Dim pic, picWH(1)
 
 ' LoadPicture를 지원하지 않는 서버가 있는데..
 ' 이유는 모르겠다.
 On Error Resume Next
 set pic = LoadPicture(imgPath)
 if Err.Number > 0 then
  response.write err.number & ": " & err.description
  picWH(0) = 500 ' set default value
  picWh(1) = 500
  getImageSize = picWH
  exit function
 end if
 picWH(0) = CLng(CDbl(pic.Width) * 24 / 635)
 picWH(1) = CLng(CDbl(pic.Height) * 24 / 635)
 
 set pic = nothing
 getImageSize = picWH
end function

' ---------------------------------------------
' 서브폴더 구하기(재귀호출)
' 함수사용전에 전역변수로 fso, path가 선언되 있어야 한다.
' strFld를 리턴한다.
' vb Script에서는 배열의 크기가 자동으로 증가되지 않기 때문에
' vbCrLf를 구분자로 사용해서 문자열로 리턴함.
' ---------------------------------------------
function getSubFolder(fso, path, strFld, depth)
 Dim fld, sfld, f, fld1, sfld1, files
 if not isObject(fso) then
  getSubFolder = "fso가 선언되지 않았습니다."
  Exit function
 end if
 
 set fld = fso.getfolder(path)
 set sfld = fld.subfolders
 
 for each f in sfld
  set fld1 = fso.getfolder(f.path)
  set files = fld1.files
  set sfld1 = fld1.subfolders
  if Len(strFld) > 0 then strFld = strFld & vbCrLf
  strFld = strFld & depth & "|" & f.name & "|" & f.path & "|" & files.count & "|" & sfld1.count
  if sfld1.count > 0 then Call getSubFolder(fso, f.path, strFld, depth+1)
 next
 getSubFolder = strFld
end function
%>

출처 : http://blog.naver.com/boxcj/150012931889