有些字符,像(<)这类的,对HTML(标准通用标记语言下的一个应用)来说是有特殊意义的,所以这些字符是不允许在文本中使用的。要在HTML中显示(<)这个字符,我们就必须使用实体字符。
| 字符 | 描述 | 实体名称 | 实体编号 |
|---|---|---|---|
| 空格 | | ||
| < | 小于号 | < | < |
| > | 大于号 | > | > |
| & | 和号 | & | & |
| " | 引号 | " | " |
| ' | 撇号 | ' (IE不支持) | ' |
| ¢ | 分 | ¢ | ¢ |
| £ | 镑 | £ | £ |
| ¥ | 人民币 | ¥ | ¥ |
<?php $str = "A 'quote' is bold"; echo htmlentities($str); echo htmlentities($str, ENT_QUOTES);
<?php $orig = "I'll \"walk\" the dog now"; // 将字符转换为 HTML 转义字符 $a = htmlentities($orig); // 将转义字符还原成 HTML 字符 $b = html_entity_decode($a);
package main
import (
"fmt"
"html"
)
func main() {
input := "Hello, & everyone!"
escaped := html.EscapeString(input)
fmt.Println(escaped)
}
package main
import (
"fmt"
"html"
)
func main() {
escapedStr := "<b>Hello, World!</b>"
unescapedStr := html.UnescapeString(escapedStr)
fmt.Println(unescapedStr)
}