代码拉取完成,页面将自动刷新
package u2
import (
"regexp"
)
var (
htmlDecodeMap = map[string]string{
"&": "&",
"<": "<",
">": ">",
""": "\"",
"'": "'",
"(": "(",
")": ")",
" ": " ",
"©": "©",
"®": "®",
"™": "™",
"×": "×",
"÷": "÷",
"—": "—",
"–": "–",
"…": "…",
"«": "«",
"»": "»",
"‹": "‹",
"›": "›",
"“": "“",
"”": "”",
"‘": "‘",
"’": "’",
"‚": "‚",
"„": "„",
"‰": "‰",
"€": "€",
"£": "£",
"¤": "¤",
"¥": "¥",
"§": "§",
"¶": "¶",
"·": "·",
"•": "•",
"†": "†",
}
htmlEncodeMap = map[string]string{
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'",
"(": "(",
")": ")",
" ": " ",
"©": "©",
"®": "®",
"™": "™",
"×": "×",
"÷": "÷",
"—": "—",
"–": "–",
"…": "…",
"«": "«",
"»": "»",
"‹": "‹",
"›": "›",
"“": "“",
"”": "”",
"‘": "‘",
"’": "’",
"‚": "‚",
"„": "„",
"‰": "‰",
"€": "€",
"£": "£",
"¤": "¤",
"¥": "¥",
"§": "§",
"¶": "¶",
"·": "·",
"•": "•",
"†": "†",
}
// 按长度降序排列,确保长实体优先匹配
htmlDecodePattern = regexp.MustCompile(`&(?:amp|lt|gt|quot|#39|#40|#41|nbsp|copy|reg|trade|times|divide|mdash|ndash|hellip|laquo|raquo|lsaquo|rsaquo|ldquo|rdquo|lsquo|rsquo|sbquo|bdquo|permil|euro|pound|curren|yen|sect|para|middot|bull|dagger);`)
htmlEncodePattern = regexp.MustCompile(`[&<>"'() ©®™×÷—–…«»‹›“”‘’‚„‰€£¤¥§¶·•†]`)
)
// HtmlDecode 将包含HTML实体的字符串转换为普通字符串。
//
// 参数:
//
// input - 包含HTML实体的字符串
//
// 返回值:
//
// 转换后的普通字符串
//
// 示例:
//
// decoded := HtmlDecode("<div>") // 返回 "<div>"
func HtmlDecode(input string) string {
return htmlDecodePattern.ReplaceAllStringFunc(input, func(match string) string {
if replacement, ok := htmlDecodeMap[match]; ok {
return replacement
}
return match
})
}
// HtmlEncode 将普通字符串转换为包含HTML实体的字符串。
//
// 参数:
//
// input - 需要编码的普通字符串
//
// 返回值:
//
// 包含HTML实体的字符串
//
// 示例:
//
// encoded := HtmlEncode("<div>") // 返回 "<div>"
func HtmlEncode(input string) string {
return htmlEncodePattern.ReplaceAllStringFunc(input, func(match string) string {
if replacement, ok := htmlEncodeMap[match]; ok {
return replacement
}
return match
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。