From 1a695482862bba1109ae8ba40759d31afa71db61 Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Wed, 20 Oct 2021 11:47:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=BF=87=E6=BB=A4html?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/string.go b/string.go index c48fb45..8588724 100755 --- a/string.go +++ b/string.go @@ -271,3 +271,27 @@ func UrlJoin(host, url string) string { return StringJoin(host, url) } + + +/** + * 去除字符串的html标签 + * @2021/10/20 + */ +func TrimHtml(src string) string { + //将HTML标签全转换成小写 + re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllStringFunc(src, strings.ToLower) + //去除STYLE + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + //去除SCRIPT + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + //去除所有尖括号内的HTML代码,并换成换行符 + re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllString(src, "\n") + //去除连续的换行符 + re, _ = regexp.Compile("\\s{2,}") + src = re.ReplaceAllString(src, "\n") + return strings.TrimSpace(src) +}