從資料庫中提取長長的文章,總是有礙網頁的排版布局。
所以,想固定地提取一部分字元,然後後面有……替代。
1,原理:
判斷文章的長度是否超過規定的長度,若超過則只顯示規定長度的大學,否則完整顯示。
2,涉及函數:
len():返回字串長度或者變數的位元組長度。
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
left():截取一個字串的前部分
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
3,主要程式:判斷內容長度是否大於給定值,據結果做相應操作
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
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," "," "),""",chr(34)),">",">"),"<","<") 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," "," "),chr(34),"""),">",">"),"<","<") End Function |