{"id":413,"date":"2011-05-03T18:15:35","date_gmt":"2011-05-03T10:15:35","guid":{"rendered":"http:\/\/zduo.me\/blog\/?p=413"},"modified":"2015-11-06T13:22:03","modified_gmt":"2015-11-06T05:22:03","slug":"php%e4%b8%8b%e7%ad%89%e9%95%bf%e6%88%aa%e5%8f%96utf8%e4%b8%ad%e8%8b%b1%e6%96%87%e6%b7%b7%e6%8e%92%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/zduo.me\/blog\/?p=413","title":{"rendered":"php\u4e0b\u7b49\u957f\u622a\u53d6UTF8\u4e2d\u82f1\u6587\u6df7\u6392\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/hi.baidu.com\/%D4%C6%CB%AF%C1%CB\/blog\/item\/84ee45eec325d7f2b2fb95a8.html<\/p>\n<pre name=\"code\" class=\"php\">\r\n\/**\r\n* \u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32(UTF-8\u4e13\u7528 \u6c49\u5b57\u548c\u5927\u5199\u5b57\u6bcd\u957f\u5ea6\u7b971\uff0c\u5176\u5b83\u5b57\u7b26\u957f\u5ea6\u7b970.5)\r\n*\r\n* @param string $string: \u539f\u5b57\u7b26\u4e32\r\n* @param int $length: \u622a\u53d6\u957f\u5ea6\r\n* @param string $etc: \u7701\u7565\u5b57\u7b26\uff08...\uff09\r\n* @return string: \u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\r\n*\/\r\n\r\nfunction cut_str_all($sourcestr, $cutlength = 80, $etc = '...')\r\n{\r\n\t$returnstr = '';\r\n\t$i = 0;\r\n\t$n = 0.0;\r\n\t$str_length = strlen($sourcestr); \/\/\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u6570\r\n\twhile ( ($n<$cutlength) and ($i<$str_length) )\r\n\t{\r\n\t\t$temp_str = substr($sourcestr, $i, 1);\r\n\t\t$ascnum = ord($temp_str); \/\/\u5f97\u5230\u5b57\u7b26\u4e32\u4e2d\u7b2c$i\u4f4d\u5b57\u7b26\u7684ASCII\u7801\r\n\t\tif ( $ascnum >= 252) \/\/\u5982\u679cASCII\u4f4d\u9ad8\u4e0e252\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 6); \/\/\u6839\u636eUTF-8\u7f16\u7801\u89c4\u8303\uff0c\u5c066\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u8ba1\u4e3a\u5355\u4e2a\u5b57\u7b26\r\n\t\t\t$i = $i + 6; \/\/\u5b9e\u9645Byte\u8ba1\u4e3a6\r\n\t\t\t$n++; \/\/\u5b57\u4e32\u957f\u5ea6\u8ba11\r\n\t\t}\r\n\t\telseif ( $ascnum >= 248 ) \/\/\u5982\u679cASCII\u4f4d\u9ad8\u4e0e248\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 5); \/\/\u6839\u636eUTF-8\u7f16\u7801\u89c4\u8303\uff0c\u5c065\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u8ba1\u4e3a\u5355\u4e2a\u5b57\u7b26\r\n\t\t\t$i = $i + 5; \/\/\u5b9e\u9645Byte\u8ba1\u4e3a5\r\n\t\t\t$n++; \/\/\u5b57\u4e32\u957f\u5ea6\u8ba11\r\n\t\t}\r\n\t\telseif ( $ascnum >= 240 ) \/\/\u5982\u679cASCII\u4f4d\u9ad8\u4e0e240\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 4); \/\/\u6839\u636eUTF-8\u7f16\u7801\u89c4\u8303\uff0c\u5c064\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u8ba1\u4e3a\u5355\u4e2a\u5b57\u7b26\r\n\t\t\t$i = $i + 4; \/\/\u5b9e\u9645Byte\u8ba1\u4e3a4\r\n\t\t\t$n++; \/\/\u5b57\u4e32\u957f\u5ea6\u8ba11\r\n\t\t}\r\n\t\telseif ( $ascnum >= 224 ) \/\/\u5982\u679cASCII\u4f4d\u9ad8\u4e0e224\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 3); \/\/\u6839\u636eUTF-8\u7f16\u7801\u89c4\u8303\uff0c\u5c063\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u8ba1\u4e3a\u5355\u4e2a\u5b57\u7b26\r\n\t\t\t$i = $i + 3 ; \/\/\u5b9e\u9645Byte\u8ba1\u4e3a3\r\n\t\t\t$n++; \/\/\u5b57\u4e32\u957f\u5ea6\u8ba11\r\n\t\t}\r\n\t\telseif ( $ascnum >= 192 ) \/\/\u5982\u679cASCII\u4f4d\u9ad8\u4e0e192\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 2); \/\/\u6839\u636eUTF-8\u7f16\u7801\u89c4\u8303\uff0c\u5c062\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u8ba1\u4e3a\u5355\u4e2a\u5b57\u7b26\r\n\t\t\t$i = $i + 2; \/\/\u5b9e\u9645Byte\u8ba1\u4e3a2\r\n\t\t\t$n++; \/\/\u5b57\u4e32\u957f\u5ea6\u8ba11\r\n\t\t}\r\n\t\telseif ( $ascnum>=65 and $ascnum<=90 and $ascnum!=73) \/\/\u5982\u679c\u662f\u5927\u5199\u5b57\u6bcd I\u9664\u5916\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 1);\r\n\t\t\t$i = $i + 1; \/\/\u5b9e\u9645\u7684Byte\u6570\u4ecd\u8ba11\u4e2a\r\n\t\t\t$n++; \/\/\u4f46\u8003\u8651\u6574\u4f53\u7f8e\u89c2\uff0c\u5927\u5199\u5b57\u6bcd\u8ba1\u6210\u4e00\u4e2a\u9ad8\u4f4d\u5b57\u7b26\r\n\t\t}\r\n\t\telseif ( !(array_search($ascnum, array(37, 38, 64, 109 ,119)) === FALSE) ) \/\/%,&#038;,@,m,w \u5b57\u7b26\u6309\uff11\u4e2a\u5b57\u7b26\u5bbd\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 1);\r\n\t\t\t$i = $i + 1; \/\/\u5b9e\u9645\u7684Byte\u6570\u4ecd\u8ba11\u4e2a\r\n\t\t\t$n++; \/\/\u4f46\u8003\u8651\u6574\u4f53\u7f8e\u89c2\uff0c\u8fd9\u4e9b\u5b57\u6761\u8ba1\u6210\u4e00\u4e2a\u9ad8\u4f4d\u5b57\u7b26\r\n\t\t}\r\n\t\telse \/\/\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5305\u62ec\u5c0f\u5199\u5b57\u6bcd\u548c\u534a\u89d2\u6807\u70b9\u7b26\u53f7\r\n\t\t{\r\n\t\t\t$returnstr = $returnstr . substr($sourcestr, $i, 1);\r\n\t\t\t$i = $i + 1; \/\/\u5b9e\u9645\u7684Byte\u6570\u8ba11\u4e2a\r\n\t\t\t$n = $n + 0.5; \/\/\u5176\u4f59\u7684\u5c0f\u5199\u5b57\u6bcd\u548c\u534a\u89d2\u6807\u70b9\u7b49\u4e0e\u534a\u4e2a\u9ad8\u4f4d\u5b57\u7b26\u5bbd...\r\n\t\t}\r\n\t}\r\n\tif ( $i < $str_length )\r\n\t{\r\n\t\t$returnstr = $returnstr . $etc; \/\/\u8d85\u8fc7\u957f\u5ea6\u65f6\u5728\u5c3e\u5904\u52a0\u4e0a\u7701\u7565\u53f7\r\n\t}\r\n\treturn $returnstr;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u7528\u5730\u5740\uff1ahttp:\/\/hi.baidu.com\/%D4%C6%CB%AF%C1%CB\/blog\/item\/84ee45eec325d7f2b2fb95a8.html \/** * \u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32(UTF-8\u4e13\u7528 \u6c49\u5b57\u548c\u5927\u5199\u5b57\u6bcd\u957f\u5ea6\u7b971\uff0c\u5176\u5b83\u5b57\u7b26\u957f\u5ea6\u7b970.5) * * @param string $string: \u539f\u5b57\u7b26\u4e32 * @param int $length: \u622a\u53d6\u957f\u5ea6 * @param string $etc: \u7701\u7565\u5b57\u7b26\uff08&#8230;\uff09 * @return string: \u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32 *\/ function cut_str_all($sourcestr, $cutlength = 80, $etc = &#8216;&#8230;&#8217;) { $returnstr = &#8221;; $i = &hellip; <a href=\"http:\/\/zduo.me\/blog\/?p=413\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[71,72,70],"_links":{"self":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/413"}],"collection":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=413"}],"version-history":[{"count":4,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/413\/revisions"}],"predecessor-version":[{"id":650,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/413\/revisions\/650"}],"wp:attachment":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}