github.com/zhongdalu/gf@v1.0.0/g/encoding/ghtml/ghtml.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // Package ghtml provides useful API for HTML content handling. 8 // 9 // HTML编码. 10 package ghtml 11 12 import ( 13 "github.com/zhongdalu/gf/third/github.com/grokify/html-strip-tags-go" 14 "html" 15 "strings" 16 ) 17 18 // 过滤掉HTML标签,只返回text内容 19 // 参考:http://php.net/manual/zh/function.strip-tags.php 20 func StripTags(s string) string { 21 return strip.StripTags(s) 22 } 23 24 // 本函数各方面都和SpecialChars一样, 25 // 除了Entities会转换所有具有 HTML 实体的字符。 26 // 参考:http://php.net/manual/zh/function.htmlentities.php 27 func Entities(s string) string { 28 return html.EscapeString(s) 29 } 30 31 // Entities 的相反操作 32 // 参考:http://php.net/manual/zh/function.html-entity-decode.php 33 func EntitiesDecode(s string) string { 34 return html.UnescapeString(s) 35 } 36 37 // 将html中的部分特殊标签转换为html转义标签 38 // 参考:http://php.net/manual/zh/function.htmlspecialchars.php 39 func SpecialChars(s string) string { 40 return strings.NewReplacer( 41 "&", "&", 42 "<", "<", 43 ">", ">", 44 `"`, """, 45 "'", "'", 46 ).Replace(s) 47 } 48 49 // 将html部分转义标签还原为html特殊标签 50 // 参考:http://php.net/manual/zh/function.htmlspecialchars-decode.php 51 func SpecialCharsDecode(s string) string { 52 return strings.NewReplacer( 53 "&", "&", 54 "<", "<", 55 ">", ">", 56 """, `"`, 57 "'", "'", 58 ).Replace(s) 59 }