{"id":528,"date":"2010-10-04T16:35:39","date_gmt":"2010-10-04T16:35:39","guid":{"rendered":""},"modified":"2010-10-04T16:35:39","modified_gmt":"2010-10-04T16:35:39","slug":"528","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=528","title":{"rendered":"php\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u4e71\u7801"},"content":{"rendered":"<p>1. \u622a\u53d6GB2312\u4e2d\u6587\u5b57\u7b26\u4e32 <br \/> <code><br \/>&lt;?php<br \/>\/\/\u622a\u53d6\u4e2d\u6587\u5b57\u7b26\u4e32&nbsp;&nbsp;<br \/>function mysubstr($str, $start, $len) {&nbsp;&nbsp;<br \/>$tmpstr = \"\";&nbsp;&nbsp;<br \/>$strlen = $start + $len;&nbsp;&nbsp;<br \/>for($i = 0; $i &lt; $strlen; $i++) {&nbsp;&nbsp;<br \/>if(ord(substr($str, $i, 1)) &gt; 0xa0) {&nbsp;&nbsp;<br \/>$tmpstr .= substr($str, $i, 2);&nbsp;&nbsp;<br \/>$i++;&nbsp;&nbsp;<br \/>} else&nbsp;&nbsp;<br \/>$tmpstr .= substr($str, $i, 1);&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>return $tmpstr;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>?&gt;&nbsp;&nbsp;<br \/> <\/code><\/p>\n<p>2. \u622a\u53d6utf8\u7f16\u7801\u7684\u591a\u5b57\u8282\u5b57\u7b26\u4e32&nbsp;&nbsp;<\/p>\n<p><code>&nbsp;&nbsp;<br \/>&lt;?php&nbsp;&nbsp;<br \/>\/\/\u622a\u53d6utf8\u5b57\u7b26\u4e32&nbsp;&nbsp;<br \/>function utf8Substr($str, $from, $len)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.&nbsp;&nbsp;<br \/>'((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',&nbsp;&nbsp;<br \/>'$1',$str);&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>?&gt;&nbsp;&nbsp;<br \/> <\/code><\/p>\n<p>3. UTF-8\u3001GB2312\u90fd\u652f\u6301\u7684\u6c49\u5b57\u622a\u53d6\u51fd\u6570&nbsp;&nbsp;<br \/> <code><br \/>&lt;?php&nbsp;&nbsp;<br \/>function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>if($code == 'UTF-8')&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$pa = \"\/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]\/\";&nbsp;&nbsp;<br \/>preg_match_all($pa, $string, $t_string);&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>if(count($t_string[0]) - $start &gt; $sublen) return join('', array_slice($t_string[0], $start, $sublen)).\"...\";&nbsp;&nbsp;<br \/>return join('', array_slice($t_string[0], $start, $sublen));&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>else&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$start = $start*2;&nbsp;&nbsp;<br \/>$sublen = $sublen*2;&nbsp;&nbsp;<br \/>$strlen = strlen($string);&nbsp;&nbsp;<br \/>$tmpstr = '';&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>for($i=0; $i&lt;$strlen; $i++)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>if($i&gt;=$start && $i&lt;($start+$sublen))&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>if(ord(substr($string, $i, 1))&gt;129)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$tmpstr.= substr($string, $i, 2);&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>else&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$tmpstr.= substr($string, $i, 1);&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>if(ord(substr($string, $i, 1))&gt;129) $i++;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>if(strlen($tmpstr)&lt;$strlen ) $tmpstr.= \"...\";&nbsp;&nbsp;<br \/>return $tmpstr;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>$str = \"abcd\u9700\u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\";&nbsp;&nbsp;<br \/>echo cut_str($str, 8, 0, 'gb2312');&nbsp;&nbsp;<br \/>?&gt;&nbsp;&nbsp;<br \/> <\/code><\/p>\n<p>4. BugFree \u7684\u5b57\u7b26\u622a\u53d6\u51fd\u6570&nbsp;&nbsp;<br \/><code> <br \/> &lt;?php&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>function sysSubStr($String,$Length,$Append = false)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>if (strlen($String) &lt;= $Length )&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>return $String;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>else&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$I = 0;&nbsp;&nbsp;<br \/>while ($I &lt; $Length)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$StringTMP = substr($String,$I,1);&nbsp;&nbsp;<br \/>if ( ord($StringTMP) &gt;=224 )&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$StringTMP = substr($String,$I,3);&nbsp;&nbsp;<br \/>$I = $I + 3;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>elseif( ord($StringTMP) &gt;=192 )&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$StringTMP = substr($String,$I,2);&nbsp;&nbsp;<br \/>$I = $I + 2;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>else&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$I = $I + 1;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>$StringLast[] = $StringTMP;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>$StringLast = implode(\"\",$StringLast);&nbsp;&nbsp;<br \/>if($Append)&nbsp;&nbsp;<br \/>{&nbsp;&nbsp;<br \/>$StringLast .= \"...\";&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>return $StringLast;&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>}&nbsp;&nbsp;<br \/>&nbsp;&nbsp;<br \/>$String = \"CodeBit.cn -- \u7b80\u5355\u3001\u7cbe\u5f69\u3001\u901a\u7528\";&nbsp;&nbsp;<br \/>$Length = \"18\";&nbsp;&nbsp;<br \/>$Append = false;&nbsp;&nbsp;<br \/>echo sysSubStr($String,$Length,$Append);&nbsp;&nbsp;<br \/>?&gt; <br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u622a\u53d6GB2312\u4e2d\u6587\u5b57\u7b26\u4e32 &lt;?php\/\/\u622a\u53d6\u4e2d\u6587\u5b57\u7b26\u4e32&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-528","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=528"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/528\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}