ホームページ  >  記事  >  ウェブフロントエンド  >  コードを通じてHTMLのエスケープ文字を識別する方法

コードを通じてHTMLのエスケープ文字を識別する方法

php中世界最好的语言
php中世界最好的语言オリジナル
2018-02-09 10:05:592089ブラウズ

今回は、HTMLのエスケープ文字をコードで識別する方法を紹介します。 コードでHTMLのエスケープ文字を識別するための注意事項は何ですか。以下は実際のケースです。

データ中に ' のような文字が現れることがあります。その特徴は次のとおりです。

真ん中の数字の列である で始まり、; で終わります。真ん中で;で終わります
たとえば、最も一般的な または同等のもの 
ブラウザがこれらの
エスケープ文字
を検出すると、エスケープされて戻りますが、コードを通じてそれらを識別するにはどうすればよいですか? org.apache.commons.lang.StringEscapeUtils.unescapeHtml に詳しい説明があります上記の最初のケースに遭遇した場合、中央は数値であり、数値 (Unicode) を char に直接変換します 2 番目のケースに遭遇した場合、中央は文字の場合、マッピングテーブルから文字に対応する番号を見つけてそれをcharに変換するだけです。コードを見ると、BASIC_ARRAY

static { 
HTML40 = new Entities(); 
fillWithHtml40Entities(HTML40); 
} 
static void fillWithHtml40Entities(Entities entities) { 
entities.addEntities(BASIC_ARRAY); 
entities.addEntities(ISO8859_1_ARRAY); 
entities.addEntities(HTML40_ARRAY); 
}

がどのように定義されているかを見ることができます。 ISO8859_1_ARRAY

private static final String[][] BASIC_ARRAY = {{"quot", "34"}, // " - double-quote 
{"amp", "38"}, // & - ampersand 
{"lt", "60"}, // < - less-than 
{"gt", "62"}, // > - greater-than 
};


HTML40_ARRAY

static final String[][] ISO8859_1_ARRAY = {{"nbsp", "160"}, // non-breaking space 
{"iexcl", "161"}, // inverted exclamation mark 
{"cent", "162"}, // cent sign 
{"pound", "163"}, // pound sign 
{"curren", "164"}, // currency sign 
{"yen", "165"}, // yen sign = yuan sign 
{"brvbar", "166"}, // broken bar = broken vertical bar 
{"sect", "167"}, // section sign 
{"uml", "168"}, // diaeresis = spacing diaeresis 
{"copy", "169"}, // � - copyright sign 
{"ordf", "170"}, // feminine ordinal indicator 
{"laquo", "171"}, // left-pointing double angle quotation mark = left pointing guillemet 
{"not", "172"}, // not sign 
{"shy", "173"}, // soft hyphen = discretionary hyphen 
{"reg", "174"}, // � - registered trademark sign 
{"macr", "175"}, // macron = spacing macron = overline = APL overbar 
{"deg", "176"}, // degree sign 
{"plusmn", "177"}, // plus-minus sign = plus-or-minus sign 
{"sup2", "178"}, // superscript two = superscript digit two = squared 
{"sup3", "179"}, // superscript three = superscript digit three = cubed 
{"acute", "180"}, // acute accent = spacing acute 
{"micro", "181"}, // micro sign 
{"para", "182"}, // pilcrow sign = paragraph sign 
{"middot", "183"}, // middle dot = Georgian comma = Greek middle dot 
{"cedil", "184"}, // cedilla = spacing cedilla 
{"sup1", "185"}, // superscript one = superscript digit one 
{"ordm", "186"}, // masculine ordinal indicator 
{"raquo", "187"}, // right-pointing double angle quotation mark = right pointing guillemet 
{"frac14", "188"}, // vulgar fraction one quarter = fraction one quarter 
{"frac12", "189"}, // vulgar fraction one half = fraction one half 
{"frac34", "190"}, // vulgar fraction three quarters = fraction three quarters 
{"iquest", "191"}, // inverted question mark = turned question mark 
{"Agrave", "192"}, // � - uppercase A, grave accent 
{"Aacute", "193"}, // � - uppercase A, acute accent 
{"Acirc", "194"}, // � - uppercase A, circumflex accent 
{"Atilde", "195"}, // � - uppercase A, tilde 
{"Auml", "196"}, // � - uppercase A, umlaut 
{"Aring", "197"}, // � - uppercase A, ring 
{"AElig", "198"}, // � - uppercase AE 
{"Ccedil", "199"}, // � - uppercase C, cedilla 
{"Egrave", "200"}, // � - uppercase E, grave accent 
{"Eacute", "201"}, // � - uppercase E, acute accent 
{"Ecirc", "202"}, // � - uppercase E, circumflex accent 
{"Euml", "203"}, // � - uppercase E, umlaut 
{"Igrave", "204"}, // � - uppercase I, grave accent 
{"Iacute", "205"}, // � - uppercase I, acute accent 
{"Icirc", "206"}, // � - uppercase I, circumflex accent 
{"Iuml", "207"}, // � - uppercase I, umlaut 
{"ETH", "208"}, // � - uppercase Eth, Icelandic 
{"Ntilde", "209"}, // � - uppercase N, tilde 
{"Ograve", "210"}, // � - uppercase O, grave accent 
{"Oacute", "211"}, // � - uppercase O, acute accent 
{"Ocirc", "212"}, // � - uppercase O, circumflex accent 
{"Otilde", "213"}, // � - uppercase O, tilde 
{"Ouml", "214"}, // � - uppercase O, umlaut 
{"times", "215"}, // multiplication sign 
{"Oslash", "216"}, // � - uppercase O, slash 
{"Ugrave", "217"}, // � - uppercase U, grave accent 
{"Uacute", "218"}, // � - uppercase U, acute accent 
{"Ucirc", "219"}, // � - uppercase U, circumflex accent 
{"Uuml", "220"}, // � - uppercase U, umlaut 
{"Yacute", "221"}, // � - uppercase Y, acute accent 
{"THORN", "222"}, // � - uppercase THORN, Icelandic 
{"szlig", "223"}, // � - lowercase sharps, German 
{"agrave", "224"}, // � - lowercase a, grave accent 
{"aacute", "225"}, // � - lowercase a, acute accent 
{"acirc", "226"}, // � - lowercase a, circumflex accent 
{"atilde", "227"}, // � - lowercase a, tilde 
{"auml", "228"}, // � - lowercase a, umlaut 
{"aring", "229"}, // � - lowercase a, ring 
{"aelig", "230"}, // � - lowercase ae 
{"ccedil", "231"}, // � - lowercase c, cedilla 
{"egrave", "232"}, // � - lowercase e, grave accent 
{"eacute", "233"}, // � - lowercase e, acute accent 
{"ecirc", "234"}, // � - lowercase e, circumflex accent 
{"euml", "235"}, // � - lowercase e, umlaut 
{"igrave", "236"}, // � - lowercase i, grave accent 
{"iacute", "237"}, // � - lowercase i, acute accent 
{"icirc", "238"}, // � - lowercase i, circumflex accent 
{"iuml", "239"}, // � - lowercase i, umlaut 
{"eth", "240"}, // � - lowercase eth, Icelandic 
{"ntilde", "241"}, // � - lowercase n, tilde 
{"ograve", "242"}, // � - lowercase o, grave accent 
{"oacute", "243"}, // � - lowercase o, acute accent 
{"ocirc", "244"}, // � - lowercase o, circumflex accent 
{"otilde", "245"}, // � - lowercase o, tilde 
{"ouml", "246"}, // � - lowercase o, umlaut 
{"divide", "247"}, // division sign 
{"oslash", "248"}, // � - lowercase o, slash 
{"ugrave", "249"}, // � - lowercase u, grave accent 
{"uacute", "250"}, // � - lowercase u, acute accent 
{"ucirc", "251"}, // � - lowercase u, circumflex accent 
{"uuml", "252"}, // � - lowercase u, umlaut 
{"yacute", "253"}, // � - lowercase y, acute accent 
{"thorn", "254"}, // � - lowercase thorn, Icelandic 
{"yuml", "255"}, // � - lowercase y, umlaut 
};


再拡張

前から見てわかる通り、エスケープ文字の真ん中の数字はUnicodeなので、この転送文字は気軽に構築でき、上記の定義では、たとえば、 の Unicode が 20013 である場合、転送文字 中 を構築すると、ブラウザによってレンダリングされた後に「中」に戻りますが、この方法で表示する必要はありません。
特殊文字

を送信するのが不便な場合は、シナリオを使用することができます


これらの事例を読んだ後は、方法を習得したと思います。さらに興味深い情報については、php 中国語 Web サイトの他の関連記事に注目してください。 !

関連読書:

テキストエリアの幅と高さを自動的に調整する方法

HTMLハイパーリンクaのクリックイベントの後、href
が指すアドレスにジャンプします

以上がコードを通じてHTMLのエスケープ文字を識別する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。