htmlspecialchars()和htmlentities()之间的区别是什么?什么时候我应该用一种或另一种?


当前回答

您可能希望使用一些Unicode字符编码,例如UTF-8和htmlspecialchars。因为不需要为“所有适用的字符”(这是htmlentities根据文档所做的)生成“HTML实体”,如果它已经在您的字符集中。

其他回答

htmlspecialchars()和htmlentities()之间的区别非常小。让我们来看一些例子:

htmlspecialchars

htmlspecialchars(string $string) takes multiple arguments where as the first argument is a string and all other arguments (certain flags, certain encodings etc. ) are optional. htmlspecialchars converts special characters in the string to HTML entities. For example if you have < br > in your string, htmlspecialchars will convert it into &lt; b &gt;. Whereas characters like µ † etc. have no special significance in HTML. So they will be not converted to HTML entities by htmlspecialchars function as shown in the below example.

echo htmlspecialchars('An example <br>'); // This will print - An example &lt; br &gt;
echo htmlspecialchars('µ †');             // This will print -  µ †

htmlentities

Htmlentities (string $string)非常类似于htmlspecialchars,需要多个参数,其中第一个参数是字符串,所有其他参数都是可选的(某些标志,某些编码等)。与htmlspecialchars不同,htmlentities不仅将字符串中的特殊字符转换为HTML实体,还将所有适用的字符转换为HTML实体。

echo htmlentities('An example <br>'); // This will print - An example &lt; br &gt;
echo htmlentities('µ †');             // This will print -  &micro; &dagger; 

PHP htmlentities文档:

这个函数在所有方面都与htmlspecialchars()相同,除了htmlentities(),所有具有HTML字符实体等价的字符都被翻译成这些实体。

从PHP文档htmlspecialchars:

某些字符在HTML中有特殊的意义,如果要保留它们的含义,就应该用HTML实体来表示。这个函数返回一个包含这些转换的字符串;所做的翻译是那些对日常web编程最有用的。如果需要翻译所有HTML字符实体,请使用htmlentities()。

区别在于编码的内容。选项是所有(实体)或“特殊”字符,如&号、双引号和单引号、小于和大于(specialchars)。

只要可能,我更喜欢使用htmlspecialchars。

例如:

    echo htmlentities('<Il était une fois un être>.');
    // Output: &lt;Il &eacute;tait une fois un &ecirc;tre&gt;.
    //                ^^^^^^^^                 ^^^^^^^

    echo htmlspecialchars('<Il était une fois un être>.');
    // Output: &lt;Il était une fois un être&gt;.
    //                ^                 ^

htmlentities -将所有适用的字符转换为HTML实体。

htmlspecialchars -将特殊字符转换为HTML实体。

翻译执行以下翻译字符:

'&'(&)变成'&' “”(双引号)在没有设置ENT_NOQUOTES时变为“"”。 只有当设置了ENT_QUOTES时,"'"(单引号)才变为'&#039;'(或')。 <(小于)变成了&lt; '>'(大于)变成'&gt;'

你可以检查下面的代码,以了解什么是htmlentities和htmlspecialchars:

https://gist.github.com/joko-wandiro/f5c935708d9c37d8940b

这是用htmlentities编码的。

implode("\t", array_values(get_html_translation_table(HTML_ENTITIES))):

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

这是用htmlspecialchars编码的。

implode("\t", array_values(get_html_translation_table(HTML_SPECIALCHARS))):

& < >

htmlspecialchars()进行最少的编码,以确保字符串不会被解析为HTML。这使得您的字符串比使用htmlentities()对所有具有编码的内容进行编码时更易于阅读。