{"id":680,"date":"2014-07-21T15:04:37","date_gmt":"2014-07-21T15:04:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=680"},"modified":"2014-07-21T15:04:37","modified_gmt":"2014-07-21T15:04:37","slug":"%e5%a6%82%e4%bd%95%e7%94%a8php%e6%a3%80%e6%b5%8b%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e5%90%a6%e4%b8%bautf-8%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/680.html","title":{"rendered":"\u5982\u4f55\u7528PHP\u68c0\u6d4b\u5b57\u7b26\u4e32\u662f\u5426\u4e3aUTF-8\u7f16\u7801"},"content":{"rendered":"<p>\u5f88\u5e38\u89c1\u7684\u4e00\u4e2a\u9700\u6c42\uff0c\u4f46\u8bf4\u5b9e\u8bdd\uff0c\u60f3\u8981\u5b8c\u5168\u68c0\u6d4b\u51fa\u6765\u4e5f\u4e0d\u73b0\u5b9e\uff0c\u53ea\u80fd\u6c42\u5f97\u4e2a\u5927\u6982\u4e86\uff0c\u4e0b\u9762\u662f\u4ece\u7f51\u4e0a\u627e\u7684\u4e00\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5bf9\u5e94\u7684\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n<h6>\u65b9\u6cd5\u4e00\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">&lt;?php\ndefine('_is_utf8_split',5000);\n\nfunction is_utf8($string) { \/\/ v1.01\n\tif (strlen($string) &gt; _is_utf8_split) {\n\t\t\/\/ Based on: http:\/\/mobile-website.mobi\/php-utf8-vs-iso-8859-1-59\n\t\tfor ($i=0,$s=_is_utf8_split,$j=ceil(strlen($string)\/_is_utf8_split);$i &lt; $j;$i++,$s+=_is_utf8_split) {\n\t\t\tif (is_utf8(substr($string,$s,_is_utf8_split)))\n\t\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t} else {\n\t\t\/\/ From http:\/\/w3.org\/International\/questions\/qa-forms-utf-8.html\n\t\treturn preg_match('%^(?:\n\t\t\t[x09x0Ax0Dx20-x7E]\t\t\t# ASCII\n\t\t\t| [xC2-xDF][x80-xBF]\t\t# non-overlong 2-byte\n\t\t\t|  xE0[xA0-xBF][x80-xBF]\t\t# excluding overlongs\n\t\t\t| [xE1-xECxEExEF][x80-xBF]{2}\t# straight 3-byte\n\t\t\t|  xED[x80-x9F][x80-xBF]\t\t# excluding surrogates\n\t\t\t|  xF0[x90-xBF][x80-xBF]{2}\t# planes 1-3\n\t\t\t| [xF1-xF3][x80-xBF]{3}\t\t# planes 4-15\n\t\t\t|  xF4[x80-x8F][x80-xBF]{2}\t# plane 16\n\t\t\t)*$%xs', $string);\n\t}\n}\n\nvar_dump(is_utf8('haha'));<\/pre>\n<h6>\u65b9\u6cd5\u4e8c\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">function detectUTF8($string) {\n    return preg_match('%(?:\n        [xC2-xDF][x80-xBF]             # non-overlong 2-byte\n        |xE0[xA0-xBF][x80-xBF]        # excluding overlongs\n        |[xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte\n        |xED[x80-x9F][x80-xBF]        # excluding surrogates\n        |xF0[x90-xBF][x80-xBF]{2}     # planes 1-3\n        |[xF1-xF3][x80-xBF]{3}         # planes 4-15\n        |xF4[x80-x8F][x80-xBF]{2}     # plane 16\n        )+%xs', $string);\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd8\u4ece\u7f51\u4e0a\u627e\u4e86\u4e2a\u5224\u65adGB2312\u7684PHP\u51fd\u6570\uff0c\u4e0d\u8fc7\u8fd8\u6ca1\u6d4b\u8bd5\u6b63\u786e\u6027\uff0c\u5148\u8bb0\u5f55\u4e00\u4e0b\u5427\uff1a<\/p>\n<pre class=\"lang:default decode:true \">function isGb2312($string) {\n\tfor($i=0; $i&lt;=127; $i++) {\n\t\tif( ($v &gt;= 228) &amp;&amp; ($v &lt; = 233) ){\n\t\t\tif( ($i+2) &gt;= (strlen($string) - 1))\n\t\t\t\treturn true;\n\t\t\t$v1 = ord( $string[$i+1] );\n\t\t\t$v2 = ord( $string[$i+2] );\n\t\t\tif( ($v1 &gt;= 128) &amp;&amp; ($v1 &lt; =191) &amp;&amp; ($v2 &gt;=128) &amp;&amp; ($v2 &lt; = 191) )\n\t\t\t\treturn false;\n\t\t\telse\n\t\t\t\treturn true;\n\t\t}\n\t}\n\treturn true;\n}<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u7f51\u5740\uff1a<\/h6>\n<div>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/910793\/detect-encoding-and-make-everything-utf-8\" target=\"_blank\">php &#8211; Detect encoding and make everything UTF-8 &#8211; Stack Overflow<\/a><\/li>\n<li><a href=\"http:\/\/www.php.net\/manual\/en\/function.mb-detect-encoding.php\" target=\"_blank\">PHP: mb_detect_encoding &#8211; Manual<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/9711701\/testing-for-utf8-strings-in-php-is-this-a-reliable-method\" target=\"_blank\">Testing for UTF8 strings in PHP, is this a reliable method? &#8211; Stack Overflow<\/a><\/li>\n<li><a href=\"http:\/\/www.overflow.biz\/blog\/lang\/pt-br\/2010\/04\/24\/how-to-detect-if-a-string-is-utf8-on-php\/\" target=\"_blank\">Detecting if a string is utf8 in php | ~ overflow ~<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u5e38\u89c1\u7684\u4e00\u4e2a\u9700\u6c42\uff0c\u4f46\u8bf4\u5b9e\u8bdd\uff0c\u60f3\u8981\u5b8c\u5168\u68c0\u6d4b\u51fa\u6765\u4e5f\u4e0d\u73b0\u5b9e\uff0c\u53ea\u80fd\u6c42\u5f97\u4e2a\u5927\u6982\u4e86\uff0c\u4e0b\u9762\u662f\u4ece\u7f51\u4e0a\u627e\u7684\u4e00\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5bf9\u5e94\u7684\u53c2\u8003 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,12],"tags":[48,255],"class_list":["post-680","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-tools","tag-php","tag-utf-8"],"views":2647,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=680"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/680\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}