php htmlentities() 与 htmlspecialchars()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:18:30  来源:igfitidea点击:

htmlentities() vs. htmlspecialchars()

php

提问by Eric Hogue

What are the differences between htmlspecialchars()and htmlentities(). When should I use one or the other?

htmlspecialchars()和之间有什么区别htmlentities()。我什么时候应该使用其中一种?

采纳答案by Thomas Owens

From the PHP documentation for htmlentities:

来自htmlentities的 PHP 文档:

This function is identical to htmlspecialchars()in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

此函数htmlspecialchars()在所有方面都相同,除了htmlentities(),所有具有 HTML 字符实体等效项的字符都被转换为这些实体。

From the PHP documentation for htmlspecialchars:

来自htmlspecialchars的 PHP 文档:

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities()instead.

某些字符在 HTML 中具有特殊意义,如果要保留其含义,应由 HTML 实体表示。此函数返回一个字符串,其中进行了一些转换;所做的翻译是那些对日常网络编程最有用的翻译。如果您需要翻译所有 HTML 字符实体,请htmlentities()改用。

The difference is what gets encoded. The choices are everything (entities) or "special" characters, like ampersand, double and single quotes, less than, and greater than (specialchars).

不同之处在于编码的内容。选择是一切(实体)或“特殊”字符,如与号、双引号和单引号、小于和大于(特殊字符)。

I prefer to use htmlspecialcharswhenever possible.

我更喜欢尽可能使用htmlspecialchars

For example:

例如:

    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;.
    //                ^                 ^

回答by Arseni Mourzenko

htmlspecialcharsmay be used:

htmlspecialchars可能用过了:

  1. When there is no need to encode all characters which have their HTML equivalents.

    If you know that the page encoding match the text special symbols, why would you use htmlentities? htmlspecialcharsis much straightforward, and produce less code to send to the client.

    For example:

    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;.
    //                ^                 ^
    

    The second one is shorter, and does not cause any problems if ISO-8859-1 charset is set.

  2. When the data will be processed not only through a browser (to avoid decoding HTML entities),

  3. If the output is XML (see the answer by Artefacto).

  1. 当不需要对所有具有 HTML 等效项的字符进行编码时。

    如果您知道页面编码与文本特殊符号匹配,为什么要使用htmlentities? 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;.
    //                ^                 ^
    

    第二个较短,如果设置了 ISO-8859-1 字符集,则不会引起任何问题。

  2. 当数据不仅会通过浏览器进行处理时(以避免解码 HTML 实体),

  3. 如果输出是 XML(请参阅Arteacto 的答案)。

回答by Berky

This is being encoded with htmlentities.

这是用htmlentities.

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

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

" & < >
? ¢ £ ¤ ¥ | § ¨ ? a ? ? - ? ˉ ° ± 2 3 ′ μ ? · ? 1 o ? ? ? ? ? à á ? ? ? ? ? ? è é ê ? ì í ? ? D ? ò ó ? ? ? × ? ù ú ? ü Y T ? à á a ? ? ? ? ? è é ê ? ì í ? ? e ? ò ó ? ? ? ÷ ? ù ú ? ü y t ? ? ? ? ? ? ? ? ? Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ ? σ τ υ φ χ ψ ω ? ? ? ? ? ? ? ? ? ? – — ‘ ' ? “ ” ? ? ? ? … ‰ ′ ″ ? ?  ̄ ? ? ? ? ? ? ← ↑ → ↓ ? ? ? ? ? ? ? ? ? ? ? ? ∈ ? ? ∏ ∑ ? ? √ ∝ ∞ ∠ ∧ ∨ ∩ ∪ ∫ ∴ ~ ? ≈ ≠ ≡ ≤ ≥ ? ? ? ? ? ⊕ ? ⊥ ? ? ? ? ? ? ? ? ? ? ? ?

" & < >
? ¢ £ ¤ ¥ | § ¨ ? 一种 ?? - ? ˉ ° ± 2 3 ′ μ ? · ? 1 ? ? ? ? ? à ? ? ? ? ? ? è é ? í ? ? ? ○ ? ? ? × ? u ? ü YT ? à a ? ? ? ? ? è é ? í ? ? ? ○ ? ? ? ÷ ? u ? 你是吗?? ? ? ? ? ? ? ? Α Β Γ Δ Ε Ζ Η Θ Ι Κ ΛΜ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ ? σ τ υ φ χ ψ ω ? ? ? ? ? ? ? ? ? ? — — ' ' ? “ ”?? ? ? … ‰ ′ ″ ? ?  ̄? ? ? ? ? ? ← ↑ → ↓ ? ? ? ? ? ? ? ? ? ? ? ? ∈ ? ? ∏ ∑ ? ? √ ∝ ∞ ∠ ∧ ∨ ∩ ∪ ∫ ∴ ~ ? ≈ ≠ ≡ ≤ ≥ ? ? ? ? ? ⊕ ? ⊥ ? ? ? ? ? ? ? ? ? ? ? ?

This is being encoded with htmlspecialchars.

这是用htmlspecialchars.

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

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

" & < >

" & < >

回答by Artefacto

Because:

因为:

  • Sometimes you're writing XML data, and you can't use HTML entities in a XML file.
  • Because htmlentitiessubstitutes more characters than htmlspecialchars. This is unnecessary, makes the PHP script less efficient and the resulting HTML code less readable.
  • 有时您正在编写 XML 数据,而您不能在 XML 文件中使用 HTML 实体。
  • 因为htmlentities替换的字符多于htmlspecialchars. 这是不必要的,会降低 PHP 脚本的效率并降低生成的 HTML 代码的可读性。

htmlentitiesis only necessary if your pages use encodings such as ASCII or LATIN-1 instead of UTF-8 and you're handling data with an encoding different from the page's.

htmlentities仅当您的页面使用 ASCII 或 LATIN-1 等编码而不是 UTF-8 并且您使用与页面不同的编码处理数据时才需要。

回答by Kmeixner

You should use htmlspecialchars($strText, ENT_QUOTES)when you just want your string to be XML and HTML safe:

htmlspecialchars($strText, ENT_QUOTES)当您只希望您的字符串是 XML 和 HTML 安全时,您应该使用:

For example, encode

例如,编码

  • & to &amp;
  • " to &quot;
  • < to &lt;
  • > to &gt;
  • ' to &#039;
  • & 到 &
  • “到”
  • < 到 <
  • > 到 >
  • ' 到 '

However, if you also have additional characters that are Unicodeor uncommon symbols in your text then you should use htmlentities() to ensure they show up properly in your HTML page.

但是,如果您的文本中还有额外的Unicode 字符或不常见的符号,那么您应该使用 htmlentities() 来确保它们在您的 HTML 页面中正确显示。

Notes:

笔记:

  • ' will only be encoded by htmlspecialchars() to &#039; if the ENT_QUOTES option is passed in. &#039; is safer to use then &apos; since older versions of Internet Explorer do not support the &apos; entity.
  • Technically, > does not need to be encoded as per the XML specification, but it is usually encoded too for consistency with the requirement of < being encoded.
  • ' 只会被 htmlspecialchars() 编码为 ' 如果传入 ENT_QUOTES 选项。 ' 使用更安全,然后使用‘; 因为旧版本的 Internet Explorer 不支持 ' 实体。
  • 从技术上讲,> 不需要按照 XML 规范进行编码,但通常也会对其进行编码,以便与 < 被编码的要求保持一致。

回答by Eric Hogue

I just found out about the get_html_translation_tablefunction. You pass it HTML_ENTITIESor HTML_SPECIALCHARSand it returns an array with the characters that will be encoded and how they will be encoded.

我刚刚发现了这个get_html_translation_table功能。您传递它HTML_ENTITIESHTML_SPECIALCHARS它返回一个数组,其中包含将被编码的字符以及它们将如何编码。

回答by grossvogel

htmlspecialchars ()does the minimum amount of encoding to ensure that your string is not parsed as HTML. This leaves your string more human-readable than it would be if you used htmlentities ()to encode absolutely everything that has an encoding.

htmlspecialchars ()进行最少的编码以确保您的字符串不会被解析为 HTML。这使您的字符串比过去htmlentities ()对所有具有编码的内容进行绝对编码时更具人类可读性。

回答by Joko Wandiro

htmlentities — Convert all applicable characters to HTML entities.

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

htmlspecialchars — Convert special characters to HTML entities.

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

The translations performed translation characters on the below:

翻译执行以下翻译字符:

  • '&' (ampersand) becomes '&amp;'
  • '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
  • "'" (single quote) becomes '&#039;' (or ') only when ENT_QUOTES is set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'
  • '&'(与号)变成了 '&'
  • '"'(双引号)变成了 '"' 当未设置 ENT_NOQUOTES 时。
  • “'”(单引号)变成 ''' (或 ') 仅当设置了 ENT_QUOTES 时。
  • '<'(小于)变成 '<'
  • '>'(大于)变成了 '>'

You can check the following code for more information about what's htmlentities and htmlspecialchars:

您可以查看以下代码以获取有关 htmlentities 和 htmlspecialchars 的更多信息:

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

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

回答by cic

You probably want to use some Unicode character encoding, for example UTF-8, and htmlspecialchars. Because there isn't anyneed to generate "HTML entities" for "all [the] applicable characters" (that is what htmlentities does according to the documentation) if it's already in your character set.

您可能想要使用一些 Unicode 字符编码,例如UTF-8和 htmlspecialchars。因为没有任何需要生成“HTML实体”为“所有[的]适用人物”(即是根据什么文件呢ヶ辆),如果它已经在你的字符集。

回答by learn2reid

One small example, I needed to have 2 client names indexed in a function:

一个小例子,我需要在一个函数中索引 2 个客户端名称:

[1] => Altisoxxce Soluxxons S.à r.l.
[5] => Joxxson & Joxxson

I originally $term = get_term_by('name', htmlentities($name), 'client');which resulted in term names that only included the ampersand array item (&) but not the accented item. But when I changed the variable setting to htmlspecialcharsboth were able to run through the function. Hope this helps!

I 最初$term = get_term_by('name', htmlentities($name), 'client');导致术语名称仅包含与号数组项 (&) 但不包含重音项。但是当我将变量设置更改为htmlspecialchars两者时都能够运行该函数。希望这可以帮助!