123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // clear
- package main
- import (
- "regexp"
- "strings"
- )
- var at = rune('&')
- var ed = rune(';')
- var lableMap = map[string]rune{
- "&": rune('&'),
- " ": rune(' '),
- ">": rune('>'),
- "<": rune('<'),
- }
- func CleanString(input string) string {
- reg := regexp.MustCompile("[^a-zA-Z0-9\u4e00-\u9fa5]+")
- cleaned := reg.ReplaceAllString(input, "")
- return cleaned
- }
- // 清洗正文
- func CleanDetailText(detail string) string {
- detail = regexp.MustCompile(`<!--[\w\W]*?-->`).ReplaceAllString(detail, "")
- detail = regexp.MustCompile(`<!--[\\n\\N]*?-->`).ReplaceAllString(detail, "")
- detail = CutLableStr(detail)
- return detail
- }
- //处理转义标签
- func CutLableStr(con string) string {
- for i := 0; i < 3; i++ {
- runes := []rune{}
- pools := []rune{}
- bpool := false
- strings.IndexFunc(con, func(s rune) bool {
- if !bpool && s == at {
- bpool = true
- pools = []rune{}
- }
- if bpool {
- pools = append(pools, s)
- if s == ed { //结束
- lb := lableMap[string(pools)]
- if lb != 0 {
- runes = append(runes, lb)
- } else {
- runes = append(runes, pools...)
- }
- bpool = false
- } else if len(pools) > 6 {
- bpool = false
- runes = append(runes, pools...)
- }
- } else {
- runes = append(runes, s)
- }
- return false
- })
- str1 := string(runes)
- if i > 0 && con == str1 {
- break
- }
- con = str1
- }
- return con
- }
|