小码哥的IT人生

首页 > PHP > php基础

PHP get_html_translation_table() 字符串 函数 详解

php基础 2022-06-07 16:59:47小码哥的IT人生shichen

PHP get_html_translation_table() 函数

实例

输出 htmlspecialchars 函数使用的翻译表:

<?php
print_r (get_html_translation_table()); // HTML_SPECIALCHARS 为默认
?>

完整实例:

<!DOCTYPE html>
<html>
<body>
<?php
print_r (get_html_translation_table()); // HTML_SPECIALCHARS 为默认
?>
</body>
</html>

 

定义和用法

get_html_translation_table() 函数返回 htmlentities()htmlspecialchars() 函数使用的翻译表。

提示:某些字符可以按照多种方式进行编码。get_html_translation_table() 函数返回最普通的编码。

语法

get_html_translation_table(function,flags,character-set)
参数 描述
function

可选。规定返回哪个翻译表。

可能的值:

  • HTML_SPECIALCHARS - 默认。翻译需要 URL 编码的字符,以便正确显示在 HTML 页面上
  • HTML_ENTITIES - 翻译所有需要 URL 编码的字符,以便正确地显示在 HTML 页面上
flags

可选。规定翻译表将包含哪种引号以及翻译表用于哪种文档类型。

可用的引号类型:

规定翻译表适用的文档类型的附加 flags:

  • ENT_COMPAT - 默认。翻译表包含双引号实体,不包含单引号实体。
  • ENT_QUOTES - 翻译表包含双引号实体和单引号实体。
  • ENT_NOQUOTES - 翻译表不包含双引号实体和单引号实体。
    • ENT_HTML401 - 默认。HTML 4.01 的翻译表。
    • ENT_HTML5 - HTML 5 的翻译表。
    • ENT_XML1 - XML 1 的翻译表。
    • ENT_XHTML - XHTML 的翻译表。
character-set

可选。字符串值,规定要使用的字符集。

允许的值:

注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。

  • UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode
  • ISO-8859-1 - 西欧
  • ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • BIG5 - 繁体中文,主要在台湾使用
  • GB2312 - 简体中文,国家标准字符集
  • BIG5-HKSCS - 带香港扩展的 Big5
  • Shift_JIS - 日语
  • EUC-JP - 日语
  • MacRoman - Mac 操作系统使用的字符集

技术细节

返回值: 以数组形式返回翻译表,原始字符作为键名,实体作为键值。
PHP 版本: 4+

更新日志:

版本 说明
PHP 5 character-set 参数的默认值改为 UTF-8。
PHP 5.4

新增了用于规定翻译表适用的文档类型的附加 flags:

  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML
PHP 5.3.4 新增了 character-set 参数。

更多实例

示例代码:

HTML_SPECIALCHARS 的翻译表:

<?php
print_r (get_html_translation_table(HTML_SPECIALCHARS));
?>

显示字符和实体名:

Array
(
  ["] => "
  [&] => &
  [<] => <
  [>] => >
)

例子 2

HTML_ENTITIES 的翻译表:

<?php
print_r (get_html_translation_table(HTML_ENTITIES));
?>

显示字符和实体名:

Array
(
  ["] => "
  [&] => &
  [<] => <
  [>] => >
  [ ] =>  
  [?] => ¡
  [?] => ¢
  [?] => £
  [?] => ¤
  [?] => ¥
  [?] => ¦
  [?] => §
  [?] => ¨
  [?] => ©
  [?] => ª
  [?] => «
  [?] => ¬
  [?] => ­
  [?] => ®
  [?] => ¯
  [?] => °
  [?] => ±
  [?] => ²
  [?] => ³
  [?] => ´
  [?] => µ
  [?] => ¶
  [?] => ·
  [?] => ¸
  [?] => ¹
  [?] => º
  [?] => »
  [?] => ¼
  [?] => ½
  [?] => ¾
  [?] => ¿
  [?] => À
  [?] => Á
  [?] => Â
  [?] => Ã
  [?] => Ä
  [?] => Å
  [?] => Æ
  [?] => Ç
  [?] => È
  [?] => É
  [?] => Ê
  [?] => Ë
  [?] => Ì
  [?] => Í
  [?] => Î
  [?] => Ï
  [?] => Ð
  [?] => Ñ
  [?] => Ò
  [?] => Ó
  [?] => Ô
  [?] => Õ
  [?] => Ö
  [?] => ×
  [?] => Ø
  [?] => Ù
  [?] => Ú
  [?] => Û
  [?] => Ü
  [?] => Ý
  [?] => Þ
  [?] => ß
  [?] => à
  [?] => á
  [?] => â
  [?] => ã
  [?] => ä
  [?] => å
  [?] => æ
  [?] => ç
  [?] => è
  [?] => é
  [?] => ê
  [?] => ë
  [?] => ì
  [?] => í
  [?] => î
  [?] => ï
  [?] => ð
  [?] => ñ
  [?] => ò
  [?] => ó
  [?] => ô
  [?] => õ
  [?] => ö
  [?] => ÷
  [?] => ø
  [?] => ù
  [?] => ú
  [?] => û
  [?] => ü
  [?] => ý
  [?] => þ
  [?] => ÿ
  [?] => Œ
  [?] => œ
  [?] => Š
  [?] => š
  [?] => Ÿ
  [?] => ƒ
  [?] => ˆ
  [?] => ˜
  [Α] => Α
  [Β] => Β
  [Γ] => Γ
  [Δ] => Δ
  [Ε] => Ε
  [Ζ] => Ζ
  [Η] => Η
  [Θ] => Θ
  [Ι] => Ι
  [Κ] => Κ
  [Λ] => Λ
  [Μ] => Μ
  [Ν] => Ν
  [Ξ] => Ξ
  [Ο] => Ο
  [Π] => Π
  [Ρ] => Ρ
  [Σ] => Σ
  [Τ] => Τ
  [Υ] => Υ
  [Φ] => Φ
  [Χ] => Χ
  [Ψ] => Ψ
  [Ω] => Ω
  [α] => α
  [β] => β
  [γ] => γ
  [δ] => δ
  [ε] => ε
  [ζ] => ζ
  [η] => η
  [θ] => θ
  [ι] => ι
  [κ] => κ
  [λ] => λ
  [μ] => μ
  [ν] => ν
  [ξ] => ξ
  [ο] => ο
  [π] => π
  [ρ] => ρ
  [?] => ς
  [σ] => σ
  [τ] => τ
  [υ] => υ
  [φ] => φ
  [χ] => χ
  [ψ] => ψ
  [ω] => ω
  [?] => ϑ
  [?] => ϒ
  [?] => ϖ
  [?] =>  
  [?] =>  
  [?] =>  
  [?] => ‌
  [?] => ‍
  [?] => ‎
  [?] => ‏
  [?] => –
  [?] => —
  [?] => ‘
  [?] => ’
  [?] => ‚
  [?] => “
  [?] => ”
  [?] => „
  [?] => †
  [?] => ‡
  [?] => •
  [?] => …
  [?] => ‰
  [′] => ′
  [″] => ″
  [?] => ‹
  [?] => ›
  [ ̄] => ‾
  [?] => ⁄
  [?] => €
  [?] => ℑ
  [?] => ℘
  [?] => ℜ
  [?] => ™
  [?] => ℵ
  [←] => ←
  [↑] => ↑
  [→] => →
  [↓] => ↓
  [?] => ↔
  [?] => ↵
  [?] => ⇐
  [?] => ⇑
  [?] => ⇒
  [?] => ⇓
  [?] => ⇔
  [?] => ∀
  [?] => ∂
  [?] => ∃
  [?] => ∅
  [?] => ∇
  [∈] => ∈
  [?] => ∉
  [?] => ∋
  [∏] => ∏
  [∑] => ∑
  [?] => −
  [?] => ∗
  [√] => √
  [∝] => ∝
  [∞] => ∞
  [∠] => ∠
  [∧] => ∧
  [∨] => ∨
  [∩] => ∩
  [∪] => ∪
  [∫] => ∫
  [∴] => ∴
  [~] => ∼
  [?] => ≅
  [≈] => ≈
  [≠] => ≠
  [≡] => ≡
  [≤] => ≤
  [≥] => ≥
  [?] => ⊂
  [?] => ⊃
  [?] => ⊄
  [?] => ⊆
  [?] => ⊇
  [⊕] => ⊕
  [?] => ⊗
  [⊥] => ⊥
  [?] => ⋅
  [?] => ⌈
  [?] => ⌉
  [?] => ⌊
  [?] => ⌋
  [?] => ⟨
  [?] => ⟩
  [?] => ◊
  [?] => ♠
  [?] => ♣
  [?] => ♥
  [?] => ♦
)

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024