2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// Package ghtml provides useful API for HTML content handling.
|
2019-01-16 13:35:16 +08:00
|
|
|
|
//
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// HTML编码.
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package ghtml
|
|
|
|
|
|
2018-09-04 13:39:12 +08:00
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"html"
|
|
|
|
|
"strings"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/internal/thirdparty/github.com/grokify/html-strip-tags-go"
|
2018-09-04 13:39:12 +08:00
|
|
|
|
)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2018-09-04 13:39:12 +08:00
|
|
|
|
// 过滤掉HTML标签,只返回text内容
|
|
|
|
|
// 参考:http://php.net/manual/zh/function.strip-tags.php
|
|
|
|
|
func StripTags(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return strip.StripTags(s)
|
2018-09-04 13:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 本函数各方面都和SpecialChars一样,
|
|
|
|
|
// 除了Entities会转换所有具有 HTML 实体的字符。
|
|
|
|
|
// 参考:http://php.net/manual/zh/function.htmlentities.php
|
|
|
|
|
func Entities(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return html.EscapeString(s)
|
2018-09-04 13:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Entities 的相反操作
|
|
|
|
|
// 参考:http://php.net/manual/zh/function.html-entity-decode.php
|
|
|
|
|
func EntitiesDecode(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return html.UnescapeString(s)
|
2018-09-04 13:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将html中的部分特殊标签转换为html转义标签
|
|
|
|
|
// 参考:http://php.net/manual/zh/function.htmlspecialchars.php
|
2017-11-23 10:21:28 +08:00
|
|
|
|
func SpecialChars(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return strings.NewReplacer(
|
|
|
|
|
"&", "&",
|
|
|
|
|
"<", "<",
|
|
|
|
|
">", ">",
|
|
|
|
|
`"`, """,
|
|
|
|
|
"'", "'",
|
|
|
|
|
).Replace(s)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 13:39:12 +08:00
|
|
|
|
// 将html部分转义标签还原为html特殊标签
|
|
|
|
|
// 参考:http://php.net/manual/zh/function.htmlspecialchars-decode.php
|
2017-11-23 10:21:28 +08:00
|
|
|
|
func SpecialCharsDecode(s string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return strings.NewReplacer(
|
|
|
|
|
"&", "&",
|
|
|
|
|
"<", "<",
|
|
|
|
|
">", ">",
|
|
|
|
|
""", `"`,
|
|
|
|
|
"'", "'",
|
|
|
|
|
).Replace(s)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|