add func html2str()
Showing
1 changed file
with
27 additions
and
0 deletions
| ... | @@ -54,6 +54,33 @@ func Substr(s string, start, length int) string { | ... | @@ -54,6 +54,33 @@ func Substr(s string, start, length int) string { |
| 54 | return string(bt[start:end]) | 54 | return string(bt[start:end]) |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | // Html2str() returns escaping text convert from html | ||
| 58 | func Html2str(html string) string { | ||
| 59 | src := string(html) | ||
| 60 | |||
| 61 | //将HTML标签全转换成小写 | ||
| 62 | re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") | ||
| 63 | src = re.ReplaceAllStringFunc(src, strings.ToLower) | ||
| 64 | |||
| 65 | //去除STYLE | ||
| 66 | re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>") | ||
| 67 | src = re.ReplaceAllString(src, "") | ||
| 68 | |||
| 69 | //去除SCRIPT | ||
| 70 | re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>") | ||
| 71 | src = re.ReplaceAllString(src, "") | ||
| 72 | |||
| 73 | //去除所有尖括号内的HTML代码,并换成换行符 | ||
| 74 | re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") | ||
| 75 | src = re.ReplaceAllString(src, "\n") | ||
| 76 | |||
| 77 | //去除连续的换行符 | ||
| 78 | re, _ = regexp.Compile("\\s{2,}") | ||
| 79 | src = re.ReplaceAllString(src, "\n") | ||
| 80 | |||
| 81 | return strings.TrimSpace(src) | ||
| 82 | } | ||
| 83 | |||
| 57 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" | 84 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" |
| 58 | func DateFormat(t time.Time, layout string) (datestring string) { | 85 | func DateFormat(t time.Time, layout string) (datestring string) { |
| 59 | datestring = t.Format(layout) | 86 | datestring = t.Format(layout) | ... | ... |
-
Please register or sign in to post a comment