Press "Enter" to skip to content

NCR与HTML Entities

Character Entities References(HTML Entities)和Numeric Character Reference(NCR)都是让你可以在页面中显示特殊的字符的编码方式, 俩者之间有什么异同呢?
一个Numeric Character Reference编码是由一个与号(&)跟着一个井号(#), 然后跟着这个字符的Unicode编码值, 最后跟着一个分号组成的, 如:

&#nnnn;
或者
&#xhhhh;

其中, nnnn是字符编码的十进制表示, 而hhhh是字符的16进制表示.
另外要注意的是x在xml中必须是小写的.而hhhh可以大小写混用, 另外nnnn和hhhh也可以有前导零.
与NCR不同, HTML Entites是使用一个与号(&),跟着这个字符的名字, 然后以分号(;)结尾来表示一个字符, 这个字符的名字必须是在HTML中已经定义的,比如:

&  //&
© //©

或者对于XML来说是在DTD中定义过的, 类似:

<!ENTITY nbsp   CDATA "&#160;" -- no-break space = non-breaking space,
                                  U+00A0 ISOnum -->
<!ENTITY iexcl  CDATA "&#161;" -- inverted exclamation mark, U+00A1 ISOnum -->
<!ENTITY cent   CDATA "&#162;" -- cent sign, U+00A2 ISOnum -->
<!ENTITY pound  CDATA "&#163;" -- pound sign, U+00A3 ISOnum -->
<!ENTITY curren CDATA "&#164;" -- currency sign, U+00A4 ISOnum -->
<!ENTITY yen    CDATA "&#165;" -- yen sign = yuan sign, U+00A5 ISOnum -->
<!ENTITY brvbar CDATA "&#166;" -- broken bar = broken vertical bar,
                                  U+00A6 ISOnum -->

其中, 名字是大小写敏感的. 比如:

&Aring; // Å
而:
&aring; // å

最后, 在PHP中, 可以通过htmlentites或者mb_convert_encoding($str, "HTML-ENTITIES", from encoding)来获得一个字符的HTML entities,
也可以通过mb_encode_numericentity(), 或者手动获取unicode值添加&#***, 来获得一个字符的NCR编码.

7 Comments

  1. essayulan
    essayulan January 30, 2019

    人们加入了不同类型的事件,这就是其中之一。如果您正在寻找,我会分享最新的活动。

  2. yufeng
    yufeng March 23, 2010

    \”最后, 在PHP中, 可以通过htmlentites或者mb_convert_encoding($str, “HTML-ENTITIES”, from encoding)来获得一个字符的HTML entities,\”
    ———————————————————————————-
    htmlentites 应该写为 htmlentities ,您老敲错了。呵呵

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.