package utils import ( "regexp" "strings" ) // StripHTML 去除HTML标签,保留纯文本 func StripHTML(html string) string { // 移除HTML标签 re := regexp.MustCompile(`<[^>]*>`) text := re.ReplaceAllString(html, "") // 替换多个空格为一个空格 text = regexp.MustCompile(`\s+`).ReplaceAllString(text, " ") // 移除特殊字符 text = strings.ReplaceAll(text, " ", " ") text = strings.ReplaceAll(text, "<", "<") text = strings.ReplaceAll(text, ">", ">") text = strings.ReplaceAll(text, "&", "&") text = strings.ReplaceAll(text, """, "\"") return strings.TrimSpace(text) } // TruncateText 截取指定长度的文本 func TruncateText(text string, length int) string { runeText := []rune(text) if len(runeText) <= length { return text } return string(runeText[:length]) }