From 30255f0316033118333566c9711ff73248385bb9 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 4 Sep 2018 13:39:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84ghtml=E5=8C=85=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E4=B8=AA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/encoding/ghtml/ghtml.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/g/encoding/ghtml/ghtml.go b/g/encoding/ghtml/ghtml.go index ded3f14c8..bbf6718db 100644 --- a/g/encoding/ghtml/ghtml.go +++ b/g/encoding/ghtml/ghtml.go @@ -7,9 +7,33 @@ // HTML编码 package ghtml -import "strings" +import ( + "strings" + "html" + "github.com/grokify/html-strip-tags-go" +) -// 将html中的特殊标签转换为html转义标签 +// 过滤掉HTML标签,只返回text内容 +// 参考:http://php.net/manual/zh/function.strip-tags.php +func StripTags(s string) string { + return strip.StripTags(s) +} + +// 本函数各方面都和SpecialChars一样, +// 除了Entities会转换所有具有 HTML 实体的字符。 +// 参考:http://php.net/manual/zh/function.htmlentities.php +func Entities(s string) string { + return html.EscapeString(s) +} + +// Entities 的相反操作 +// 参考:http://php.net/manual/zh/function.html-entity-decode.php +func EntitiesDecode(s string) string { + return html.UnescapeString(s) +} + +// 将html中的部分特殊标签转换为html转义标签 +// 参考:http://php.net/manual/zh/function.htmlspecialchars.php func SpecialChars(s string) string { return strings.NewReplacer( "&", "&", @@ -20,7 +44,8 @@ func SpecialChars(s string) string { ).Replace(s) } -// 将html转义标签还原为html特殊标签 +// 将html部分转义标签还原为html特殊标签 +// 参考:http://php.net/manual/zh/function.htmlspecialchars-decode.php func SpecialCharsDecode(s string) string { return strings.NewReplacer( "&", "&",