ASP中新闻标题太长时,省略号显示过长内容

标题太长时,往往会把界面撑开,影响美观,所以我们需要省略过长的文字内容。

以下调用方法
<%= 函数名(Rs("字段"),标题长度) %>
便如:
<%= InterceptString(Rs("title"),30) %>

方法一:

Function InterceptString(txt,length)
    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

这种方法只是截取长度。不会有省略号。下面介绍一种有省略号的

方法二:

function cLeft(str,n)
dim str1,str2,alln,Islefted
str2 = ""
alln = 0
str1 = str
Islefted = false
if isnull(str) then
cleft = ""
exit function
end if
for i = 1 to len(str1)
nowstr = mid(str1,i,1)
if asc(nowstr)<0 then
alln = alln + 2
else
alln = alln + 1
end if
if (alln<=n) then
str2 = str2 & nowstr
else
Islefted = true
exit for
end if
next
if Islefted then
str2 = str2 & "..."
end if
cleft = str2
end function

 

=====================================================

另外在网上搜集到其它两种贴出来

方法一、在ASP中使用mid()函数

<%=mid(rs("title"),1,10)%>

该代码表示,显示从第一个字符开始,长度为10的标题内容
(这种方法非常简单,但多余内容不用省略号代替)

方法二、ASP判断语句
比较完美的方法

程序代码
<%if len(rs.Fields.Item("title").Value)>10then
response.Write left(rs.Fields.Item("title").Value,10)&"......"
else
response.Write rs.Fields.Item("title").Value
end if %>

引用地址:

post by 不懂戀愛魚兒 | 2010年5月10日 | 归档于 [Asp技术]
标签: asp function
  1. qukiy
    2010-05-29 18:57| #1

    多谢.一次性解决问题

发表评论