如何将长的标题用省略号收尾

所属分类: 网络编程 / ASP编程 阅读数: 1306
收藏 0 赞 0 分享
从数据库中提取长长的文章,总是有碍网页的排版布局。

所以,想固定地提取一部分字符,然后后面有……替代。

1,原理:
判断文章的长度是否超过规定的长度,若超过则只显示规定长度的大学,否则完整显示。

2,涉及函数:

len():返回字符串长度或者变量的字节长度。

left():截取一个字符串的前部分

3,主要程序:判断内容长度是否大于给定值,据结果做相应操作

4,ASP中应用
以上是在客户端脚本调试,和ASP也是大同小异:最主要的是函数功能。


<%
text=rs("content")  '将数据库字段值赋到某变量上
i=10                      '定义固定大小
if len(text)>i then   '如果文本长度大于给定的值
text=left(text,i)     '则提取前段的i位的字符串
response.write (text&"...")
else
response.write (text)
end if
%>


5,为了方便,做成函数


<%
function conleft(content,i)
if len(content)>i then  
content=left(content,i)    
response.write (content&"...")
else
response.write (content)
end if
end function
%>


以上为函数,下面就可以直接调用。


<%call conleft(rs("content"),10)%>


OK,相信以后遇到这些问题应该是NO PROBLEM 

为了解决中英文截取的问题,建议大家使用如下函数:


Function gotTopic(str,strlen)
    if str="" then
        gotTopic=""
        exit function
    end if
    dim l,t,c, i
    str=replace(replace(replace(replace(str,"&nbsp;"," "),"&quot;",chr(34)),"&gt;",">"),"&lt;","<")
    l=len(str)
    t=0
    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
            gotTopic=left(str,i) & "…"
            exit for
        else
            gotTopic=str
        end if
    next
    gotTopic=replace(replace(replace(replace(gotTopic," ","&nbsp;"),chr(34),"&quot;"),">","&gt;"),"<","&lt;")
End Function

更多精彩内容其他人还在看

asp中去除内容HTML标签的三个function函数

asp中去除内容HTML标签的三个function函数
收藏 0 赞 0 分享

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解
收藏 0 赞 0 分享

方便的大家admin及admin888 经过 md5加密后16位和32位代码

方便的大家admin及admin888 经过 md5加密后16位和32位代码
收藏 0 赞 0 分享

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集
收藏 0 赞 0 分享

asp动态级联菜单代码

asp动态级联菜单代码
收藏 0 赞 0 分享

ASP中经常使用的SQL语句与教程说明

ASP中经常使用的SQL语句与教程说明
收藏 0 赞 0 分享

实例分析之用ASP编程实现网络内容快速查找的代码

实例分析之用ASP编程实现网络内容快速查找的代码
收藏 0 赞 0 分享

服务端 VBScript 与 JScript 几个相同特性的写法与示例

服务端 VBScript 与 JScript 几个相同特性的写法与示例
收藏 0 赞 0 分享

ASP 环境下 VBS 事件应用 示例代码

ASP 环境下 VBS 事件应用 示例代码
收藏 0 赞 0 分享

asp 之上传漏洞终结篇

asp 之上传漏洞终结篇
收藏 0 赞 0 分享
查看更多