asp中如何限制文本显示的字数啊?
<divclass="incontentblock"><divclass="title">企业简介</div><divclass="content"><%=mzwords...
<div class="incontent block">
<div class="title">
企 业 简 介
</div>
<div class="content"><% =mzwords %></div>
</div>
怎样控制mzwords的字数只显示前面100个字呢? 展开
<div class="title">
企 业 简 介
</div>
<div class="content"><% =mzwords %></div>
</div>
怎样控制mzwords的字数只显示前面100个字呢? 展开
2个回答
展开全部
在页面加入加这个函数:
''截取定长字符串,一个汉字两个字节
Function CutStr(Str,length)
On Error Resume Next
dim strContent,HtmlStr:HtmlStr=Trim(Str)
If isNull(HtmlStr) Or HtmlStr = "" Then
CutStr= "为空"
Exit Function
End If
Dim J,y:y= 0
if Length=0 then''长度为0时全部取出
CutStr=HtmlStr
Exit Function
End If
HtmlStr = Replace(HtmlStr, " ", " ")
HtmlStr = Replace(HtmlStr, """, Chr(34))
HtmlStr = Replace(HtmlStr, "'", Chr(39))
HtmlStr = Replace(HtmlStr, "{", Chr(123))
HtmlStr = Replace(HtmlStr, "}", Chr(125))
HtmlStr = Replace(HtmlStr, "$", Chr(36))
HtmlStr = Replace(HtmlStr, "…", "…")
HtmlStr = Replace(HtmlStr, "‘", "‘")
HtmlStr = Replace(HtmlStr, "’", "’")
HtmlStr = Replace(HtmlStr, "“", "“")
HtmlStr = Replace(HtmlStr, "”", "”")
HtmlStr = Replace(HtmlStr, "×", "×")
HtmlStr = Replace(HtmlStr, "√", "√")
HtmlStr = Replace(HtmlStr, ">", ">")
HtmlStr = Replace(HtmlStr, "<", "<")
For J= 1 To Len(HtmlStr)
If Asc(Mid(HtmlStr,J,1)) < 0 Or Asc(Mid(HtmlStr,J, 1)) > 255 Then
y = y + 2
Else
y = y + 1
End If
If y >= length Then
HtmlStr= Left(Trim(HtmlStr),J)&"..."
Exit For
End If
Next
strContent=HtmlStr
strContent = Replace(strContent, Chr(34), """)
strContent = Replace(strContent, Chr(39), "'")
strContent = Replace(strContent, Chr(36), "$")
strContent = Replace(strContent, Chr(123), "{")
strContent = Replace(strContent, Chr(125), "}")
strContent = Replace(strContent, ">", ">")
strContent = Replace(strContent, "<", "<")
CutStr=replace(strContent,chr(10),"")
End Function
引用方法是:<% =CutStr(mzwords,100)%>
这个函数可以对英文与中文字符正确识别并截取。只要left()无法正常中文和英文字数。
''截取定长字符串,一个汉字两个字节
Function CutStr(Str,length)
On Error Resume Next
dim strContent,HtmlStr:HtmlStr=Trim(Str)
If isNull(HtmlStr) Or HtmlStr = "" Then
CutStr= "为空"
Exit Function
End If
Dim J,y:y= 0
if Length=0 then''长度为0时全部取出
CutStr=HtmlStr
Exit Function
End If
HtmlStr = Replace(HtmlStr, " ", " ")
HtmlStr = Replace(HtmlStr, """, Chr(34))
HtmlStr = Replace(HtmlStr, "'", Chr(39))
HtmlStr = Replace(HtmlStr, "{", Chr(123))
HtmlStr = Replace(HtmlStr, "}", Chr(125))
HtmlStr = Replace(HtmlStr, "$", Chr(36))
HtmlStr = Replace(HtmlStr, "…", "…")
HtmlStr = Replace(HtmlStr, "‘", "‘")
HtmlStr = Replace(HtmlStr, "’", "’")
HtmlStr = Replace(HtmlStr, "“", "“")
HtmlStr = Replace(HtmlStr, "”", "”")
HtmlStr = Replace(HtmlStr, "×", "×")
HtmlStr = Replace(HtmlStr, "√", "√")
HtmlStr = Replace(HtmlStr, ">", ">")
HtmlStr = Replace(HtmlStr, "<", "<")
For J= 1 To Len(HtmlStr)
If Asc(Mid(HtmlStr,J,1)) < 0 Or Asc(Mid(HtmlStr,J, 1)) > 255 Then
y = y + 2
Else
y = y + 1
End If
If y >= length Then
HtmlStr= Left(Trim(HtmlStr),J)&"..."
Exit For
End If
Next
strContent=HtmlStr
strContent = Replace(strContent, Chr(34), """)
strContent = Replace(strContent, Chr(39), "'")
strContent = Replace(strContent, Chr(36), "$")
strContent = Replace(strContent, Chr(123), "{")
strContent = Replace(strContent, Chr(125), "}")
strContent = Replace(strContent, ">", ">")
strContent = Replace(strContent, "<", "<")
CutStr=replace(strContent,chr(10),"")
End Function
引用方法是:<% =CutStr(mzwords,100)%>
这个函数可以对英文与中文字符正确识别并截取。只要left()无法正常中文和英文字数。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询