DoTrimProperly:HTML 編碼 + 長度截斷 + 可選省略符號
Function DoTrimProperly(str, nNamedFormat, properly, pointed, points)
Dim strRet
strRet = Server.HTMLEncode(str) ' 內建編碼 < > & " '
strRet = Replace(strRet, vbCrLf, "
")
strRet = Replace(strRet, vbTab, "")
If Len(strRet) > nNamedFormat Then
strRet = Left(strRet, nNamedFormat)
If properly = 1 Then
' 避免截斷單字:按空白分割重組
Dim TempArray, n
TempArray = Split(strRet, " ")
strRet = ""
For n = 0 To UBound(TempArray) - 1
strRet = strRet & " " & TempArray(n)
Next
End If
If pointed = 1 Then strRet = strRet & points
End If
DoTrimProperly = strRet
End Function
FormatStr 版本一:編碼 + 換行轉 br/p
Function FormatStr(String)
On Error Resume Next
String = Replace(String, Chr(13), "")
String = Replace(String, "<", "<")
String = Replace(String, ">", ">")
String = Replace(String, Chr(10) & Chr(10), "
")
String = Replace(String, Chr(10), "
")
FormatStr = String
End Function
FormatStr 版本二:空白轉 nbsp + 編碼 + 換行
Function FormatStr(String)
On Error Resume Next
String = Replace(String, " ", " ")
String = Replace(String, Chr(13), " ")
String = Replace(String, Chr(10) & Chr(10), "
")
String = Replace(String, Chr(10), "
")
String = Replace(String, "<", "<")
String = Replace(String, ">", ">")
FormatStr = String
End Function
比較與建議
DoTrimProperly 較完整:使用內建 Server.HTMLEncode、支援智慧截斷(不斷字)、可加省略符號。
- 兩個
FormatStr 同名會衝突,需擇一或改名。
- 版本二將空白轉
會導致文字無法換行排版,僅適合單行顯示。
- 建議統一使用
Server.HTMLEncode + CSS white-space: pre-wrap 處理換行,分離資料與呈現。