当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP按照字数限制自动截取标题内容

ASP
ASP汉字转拼音,支持自定义特殊词语
终于找到了ASP下标越界的解决方法
ASP实现长文章手动分页的代码
如何节约程序开发中的时间
防sql注入代码
asp连接远程mssql数据库代码
fso检测文件、磁盘、文件夹是否存在代码
asp随机获取数据库中的记录代码
利用fso显示某一文件夹中的所有内容
利用asp获取客户端真实的IP地址
Cookies常用命令简介
将多行区域表单中的内容换成html代码
rs.open sql,conn,1,1中各参数的意义
动态图形验证码
常用的asp代码
ASP如何得到字符串的每一位字符
ASP用户登录代码
网站静态页面生成方法
fso生成有多行内容的html文件
fso向html文件追加内容

ASP按照字数限制自动截取标题内容


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 44 ::
收藏到网摘: n/a

通常在显示新闻标题的时候,有时候标题中会出现汉字和英文共存,直接用left截字符串会出现两条新闻标题长度不一,使用这个函数可以解决这个问题。


<%
'**************************************************
'函数名:gotTopic
'作  用:截字符串,汉字一个算两个字符,英文算一个字符
'参  数:str   ----原字符串
'       strlen ----截取长度
'返回值:截取后的字符串
'**************************************************
Function gotTopic(ByVal str, ByVal strlen)
    If str = "" Then
        gotTopic = ""
        Exit Function
    End If
    Dim l, t, c, i, strTemp
    str = Replace(Replace(Replace(Replace(str, " ", " "), """, Chr(34)), ">", ">"), "&lt;", "<")
    l = Len(str)
    t = 0
    strTemp = str
    strlen = CLng(strlen)
    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
            strTemp = Left(str, i)
            Exit For
        End If
    Next
    If strTemp <> str Then
        strTemp = strTemp & "…"
    End If
    gotTopic = Replace(Replace(Replace(Replace(strTemp, " ", " "), Chr(34), """), ">", ">"), "<", "&lt;")
End Function
%>
 <%
 str="一共11111w有汉字"
 str1="一共有五汉字"
 response.write "gotTopic
"
 response.write gotTopic(str,10)&"
"&gotTopic(str1,10)&"
"
 response.write "left
"
 response.write Left(str,5)&"
"&Left(str1,5)
 response.end
%>