|
@@ -966,10 +966,26 @@ func InterceptHtmlLength(body string, length int) (newbody string) {
|
|
|
return
|
|
|
}
|
|
|
bodyText := doc.Text()
|
|
|
- if len(bodyText) < length {
|
|
|
+ //if len(bodyText) < length {
|
|
|
+ // length = len(bodyText)
|
|
|
+ //}
|
|
|
+ //newbody = bodyText[0:length]
|
|
|
+
|
|
|
+ totalCharCount := utf8.RuneCountInString(bodyText)
|
|
|
+ if totalCharCount < length {
|
|
|
length = len(bodyText)
|
|
|
}
|
|
|
- newbody = bodyText[0:length]
|
|
|
+ // 计算前15个汉字所需的字节位置
|
|
|
+ hanziCount := 0
|
|
|
+ byteIndex := 0
|
|
|
+ for byteIndex < len(bodyText) && hanziCount < length {
|
|
|
+ r, size := utf8.DecodeRuneInString(bodyText[byteIndex:])
|
|
|
+ if r != utf8.RuneError {
|
|
|
+ hanziCount++
|
|
|
+ }
|
|
|
+ byteIndex += size
|
|
|
+ }
|
|
|
+ newbody = bodyText[:byteIndex] + "…"
|
|
|
return
|
|
|
}
|
|
|
|