[推荐]ASP编程通用函数收藏大全第1/2页


区分中英文长度,限长截断标题字符

复制代码 代码如下:

<% 
'****************************** 
'函数:InterceptString(txt,length) 
'参数:txt,待判断截取的标题字符串;length,标题长度 
'作者:阿里西西 
'日期:2007/7/12 
'描述:区分中英文,限长截断标题字符 
'示例:<%=InterceptString("欢迎光临阿里西西WEB开发网站",8)%> 
'****************************** 
Function InterceptString(txt,length) 
 dim x,y,ii 
 txt=trim(txt) 
 x = len(txt) 
 y = 0 
 if x >= 1 then 
  for ii = 1 to x 
   if asc(mid(txt,ii,1)) < 0 or asc(mid(txt,ii,1)) >255 then '如果是汉字 
    y = y + 2 
   else 
    y = y + 1 
   end if 
   if y >= length then 
    txt = left(trim(txt),ii) '字符串限长 
    exit for 
   end if 
  next 
  InterceptString = txt 
 else 
  InterceptString = "" 
 end if 
End Function 
%>

复制代码 代码如下:

<%
'******************************
'函数:strLength(str)
'参数:str,待判断长度的字符串
'作者:阿里西西
'日期:2007/7/12
'描述:求字符串长度。汉字算两个字符,英文算一个字符
'示例:<%=strLength("欢迎光临阿里西西")%>
'******************************
function strLength(str)
 ON ERROR RESUME NEXT
 dim WINNT_CHINESE
 WINNT_CHINESE    = (len("中国")=2)
 if WINNT_CHINESE then
        dim l,t,c
        dim i
        l=len(str)
        t=l
        for i=1 to l
         c=asc(mid(str,i,1))
            if c<0 then c=c+65536
            if c>255 then
                t=t+1
            end if
        next
        strLength=t
    else 
        strLength=len(str)
    end if
    if err.number<>0 then err.clear
end function
%>

复制代码 代码如下:

采集获取远程页面的内容<%
'******************************
'函数:GetURL(url) 
'参数:url,远程页面的网址,必须输入完整格式的网址
'作者:阿里西西
'日期:2007/7/12
'描述:采集获取远程页面的内容,很多小偷和采集程序都用到
'示例:<%=GetURL(http://www.alixixi.com/index.html)%>
'******************************
Function GetURL(url) 
Set Retrieval = CreateObject("Microsoft.XMLHTTP") 
With Retrieval 
.Open "GET", url, False
.Send 
GetURL = bytes2bstr(.responsebody)
'对取得信息进行验证,如果信息长度小于100则说明截取失败
if len(.responsebody)<100 then
response.write "获取远程文件 <a href="&url&" target=_blank>"&url&"</a> 失败。"
response.end
end if
End With 
Set Retrieval = Nothing 
End Function
' 二进制转字符串,否则会出现乱码的!
function bytes2bstr(vin) 
strreturn = "" 
for i = 1 to lenb(vin) 
thischarcode = ascb(midb(vin,i,1)) 
if thischarcode < &h80 then 
strreturn = strreturn & chr(thischarcode) 
else 
nextcharcode = ascb(midb(vin,i+1,1)) 
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode)) 
i = i + 1 
end if 
next 
bytes2bstr = strreturn 
end function 
%>