ASP给长的标题加省略号...

作者:cnBruce 时间:2008-02-22 14:39:00 

数据库中提取长长的文章,总是有碍网页的排版布局。

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

相关推荐:当标题不能显示完整的时候


1、原理:

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


2、涉及函数:


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


<script language="vbs">
cnbruce="my name is cnbruce"
len_cn=len(cnbruce)
alert(len_cn)
</script> 


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

<script language="vbs">
cnbruce="my name is cnbruce"
left_cn=left(cnbruce,10)
alert(left_cn)
</script> 


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

<script language="vbs">
text="123 4567 8fds90 abcde fghxcfv"
i=10
if len(text)>i then   '如果文本长度大于给定的值
  text=left(text,i)     '则提取前段的i位的字符串
alert (text&"...")
else
alert (text)
end if
</script> 


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 

标签:标题,省略号,left,asp
0
投稿

猜你喜欢

  • asp全面解析Server对象

    2008-10-19 17:24:00
  • 触手生春【4.13】CSS中的伪元素选择符

    2008-11-11 13:10:00
  • 在SQL Server 2005数据库中更改数据架构

    2009-01-19 13:06:00
  • asp多关键词查询方案

    2008-05-09 12:24:00
  • Linux ORCLE数据库增量备份脚本

    2009-11-21 09:43:00
  • 制作全局临时表

    2010-06-11 13:36:00
  • asp中去除html中style,javascript,css代码

    2011-02-16 11:18:00
  • 30个出色的分页设计

    2009-05-12 17:49:00
  • MySQL Order By Rand()效率

    2011-01-04 19:34:00
  • CSS制作11种风格不同的特效文字

    2010-10-20 20:08:00
  • Js实现仿msn的右下角popup提示窗口

    2007-12-27 20:30:00
  • 实例讲解在MySQL中如何导出整个数据库

    2009-09-01 10:03:00
  • ajax代理程序,自动判断字符编码

    2007-11-04 13:17:00
  • ASP在服务器自动解压RAR文件

    2010-04-24 16:06:00
  • 解析CSS列表样式属性list-style

    2009-03-26 13:16:00
  • asp文章干扰码实现方法

    2007-08-19 18:07:00
  • discuz 2.0整合asp系统,用户添加函数

    2011-04-02 11:08:00
  • 两个不太常用的 CSS Hack

    2008-06-27 12:49:00
  • asp被杀毒软件误删的解决方法

    2011-04-11 11:16:00
  • 10条影响CSS渲染速度的写法与建议

    2008-09-09 22:02:00
  • asp之家 网络编程 m.aspxhome.com