-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhtml.go
58 lines (50 loc) · 1.57 KB
/
xhtml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package ghtml provides useful API for HTML content handling.
package xhtml
import (
strip "github.com/go-xe2/third/github.com/grokify/html-strip-tags-go"
"html"
"strings"
)
// 过滤掉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(
"&", "&",
"<", "<",
">", ">",
`"`, """,
"'", "'",
).Replace(s)
}
// 将html部分转义标签还原为html特殊标签
// 参考:http://php.net/manual/zh/function.htmlspecialchars-decode.php
func SpecialCharsDecode(s string) string {
return strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
""", `"`,
"'", "'",
).Replace(s)
}