<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>编码 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E7%BC%96%E7%A0%81/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 01 Oct 2012 09:56:38 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>PHP判断UTF-8与GBK编码</title>
		<link>https://www.mudbest.com/php%e5%88%a4%e6%96%adutf-8%e4%b8%8egbk%e7%bc%96%e7%a0%81/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Thu, 19 Apr 2012 07:26:42 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gbk]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[编码]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1052</guid>

					<description><![CDATA[判断中文和编码有关 gbk是双字节，utf8是三字节，可以根据 中文的范围来判断 编码范围1. GBK (GB ... <a title="PHP判断UTF-8与GBK编码" class="read-more" href="https://www.mudbest.com/php%e5%88%a4%e6%96%adutf-8%e4%b8%8egbk%e7%bc%96%e7%a0%81/" aria-label="阅读 PHP判断UTF-8与GBK编码">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>判断中文和编码有关 gbk是双字节，utf8是三字节，可以根据 中文的范围来判断   <span id="more-1052"></span></p>
<pre class="brush: php; title: ; notranslate">
// 返回: true - 含GB编码 false - 为UTF-8编码
function is_gb2312($str) {
	for($i = 0; $i &lt; strlen ( $str ); $i ++) {
		$v = ord ( $str &#x5B;$i] );
		if ($v &gt; 127) {
			if (($v &gt;= 228) &amp;&amp; ($v &lt;= 233)) {
				if (($i + 2) &gt;= (strlen ( $str ) - 1))
					return true; // not enough characters
				$v1 = ord ( $str &#x5B;$i + 1] );
				$v2 = ord ( $str &#x5B;$i + 2] );
				if (($v1 &gt;= 128) &amp;&amp; ($v1 &lt;= 191) &amp;&amp; ($v2 &gt;= 128) &amp;&amp; ($v2 &lt;= 191)){ // utf编码
					return false;
				}else{
					return true;
				}
			}
		}
	}
	return true;
}
</pre>
<p>编码范围1. GBK (GB2312/GB18030)<br />
\x00-\xff GBK双字节编码范围<br />
\x20-\x7f ASCII<br />
\xa1-\xff 中文<br />
\x80-\xff 中文  </p>
<p>2. UTF-8 (Unicode)<br />
\u4e00-\u9fa5 (中文)<br />
\x3130-\x318F (韩文<br />
\xAC00-\xD7A3 (韩文)<br />
\u0800-\u4e00 (日文)<br />
ps: 韩文是大于[\u9fa5]的字符    </p>
<p>一、正则例子:   </p>
<pre class="brush: php; title: ; notranslate">
preg_replace(”/(&#x5B;\x80-\xff])/”,”&quot;,$str);  
preg_replace(”/(&#x5B;u4e00-u9fa5])/”,”&quot;,$str); 
</pre>
<p>二、代码例子</p>
<pre class="brush: php; title: ; notranslate"> 
//判断内容里有没有中文 -GBK (PHP)        
function check_is_chinese($s){        
      return preg_match('/&#x5B;\x80-\xff]./', $s);        
 } 
</pre>
<pre class="brush: php; title: ; notranslate">
 //获取字符串长度 -GBK (PHP)        
 function gb_strlen($str){        
      $count = 0;        
      for($i=0; $i&lt;strlen($str); $i++){        
          $s = substr($str, $i, 1);        
          if (preg_match(&quot;/&#x5B;\x80-\xff]/&quot;, $s)) ++$i;        
            ++$count;        
      }        
      return $count;        
 }  
</pre>
<pre class="brush: php; title: ; notranslate">     
//截取字符串字 串 -GBK (PHP)        
 function gb_substr($str, $len){        
      $count = 0;        
      for($i=0; $i&lt;strlen($str); $i++){        
          if($count == $len) break;        
          if(preg_match(&quot;/&#x5B;\x80-\xff]/&quot;, substr($str, $i, 1))) ++$i;        
            ++$count;                
      }        
      return substr($str, 0, $i);        
 }        
</pre>
<pre class="brush: php; title: ; notranslate">  
 //统计字符串长度 -UTF8 (PHP)        
 function utf8_strlen($str) {        
      $count = 0;        
      for($i = 0; $i &lt; strlen($str); $i++){        
          $value = ord($str&#x5B;$i]);        
          if($value &gt; 127) {        
              $count++;        
              if($value &gt;= 192 &amp;&amp; $value &lt;= 223) $i++;        
              elseif($value &gt;= 224 &amp;&amp; $value &lt;= 239) $i = $i + 2;        
              elseif($value &gt;= 240 &amp;&amp; $value &lt;= 247) $i = $i + 3;        
              else die('Not a UTF-8 compatible string');        
          }        
          $count++;        
      }        
      return $count;        
 }        
 </pre>
<pre class="brush: php; title: ; notranslate"> 
 //截取字符串 -UTF8(PHP)        
 function utf8_substr($str,$position,$length){        
      $start_position = strlen($str);        
      $start_byte = 0;        
      $end_position = strlen($str);        
      $count = 0;        
      for($i = 0; $i &lt; strlen($str); $i++){        
          if($count &gt;= $position &amp;&amp; $start_position &gt; $i){        
              $start_position = $i;        
              $start_byte = $count;        
          }        
          if(($count-$start_byte)&gt;=$length) {        
              $end_position = $i;        
              break;        
          }            
          $value = ord($str&#x5B;$i]);        
          if($value &gt; 127){        
              $count++;        
              if($value &gt;= 192 &amp;&amp; $value &lt;= 223) $i++;        
              elseif($value &gt;= 224 &amp;&amp; $value &lt;= 239) $i = $i + 2;        
              elseif($value &gt;= 240 &amp;&amp; $value &lt;= 247) $i = $i + 3;        
              else die('Not a UTF-8 compatible string');        
          }        
          $count++;        
       
      }        
      return(substr($str,$start_position,$end_position-$start_position));        
 }        
</pre>
<pre class="brush: php; title: ; notranslate">       
 //判断是否是有韩文 -UTF- 8 (JavaScript)        
 function checkKoreaChar(str) {        
      for(i=0; i&lt;str.length; i++) {        
          if(((str.charCodeAt(i) &gt; 0x3130 &amp;&amp; str.charCodeAt(i) &lt; 0x318F)
|| (str.charCodeAt(i) &gt;= 0xAC00 &amp;&amp; str.charCodeAt(i) &lt;= 0xD7A3))) {        
              return true;        
          }        
      }        
      return false;        
 }        
</pre>
<pre class="brush: php; title: ; notranslate">
 //判断是否有中文字 符 -GBK (JavaScript)        
 function check_chinese_char(s){        
      return (s.length != s.replace(/&#x5B;^\x00-\xff]/g,&quot;**&quot;).length);        
 }          
</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>判断字符串编码是否为utf-8函数</title>
		<link>https://www.mudbest.com/%e5%88%a4%e6%96%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%bc%96%e7%a0%81%e6%98%af%e5%90%a6%e4%b8%bautf-8%e5%87%bd%e6%95%b0/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Fri, 08 Jul 2011 16:36:34 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[编码]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=706</guid>

					<description><![CDATA[&#60;?php //判断字符串是否为utf-8 //2011-07-09 function CodingUf ... <a title="判断字符串编码是否为utf-8函数" class="read-more" href="https://www.mudbest.com/%e5%88%a4%e6%96%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%bc%96%e7%a0%81%e6%98%af%e5%90%a6%e4%b8%bautf-8%e5%87%bd%e6%95%b0/" aria-label="阅读 判断字符串编码是否为utf-8函数">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<pre class="brush: php; title: ; notranslate">
&lt;?php
//判断字符串是否为utf-8
//2011-07-09
function CodingUft($string) {
	return preg_match 
	( '%^
	(?:          
	&#x5B;\x09\x0A\x0D\x20-\x7E]            # ASCII        
	| &#x5B;\xC2-\xDF]&#x5B;\x80-\xBF]             # non-overlong 2-byte        
	|  \xE0&#x5B;\xA0-\xBF]&#x5B;\x80-\xBF]        # excluding overlongs        
	| &#x5B;\xE1-\xEC\xEE\xEF]&#x5B;\x80-\xBF]{2}  # straight 3-byte        
	|  \xED&#x5B;\x80-\x9F]&#x5B;\x80-\xBF]        # excluding surrogates        
	|  \xF0&#x5B;\x90-\xBF]&#x5B;\x80-\xBF]{2}     # planes 1-3        
	| &#x5B;\xF1-\xF3]&#x5B;\x80-\xBF]{3}          # planes 4-15        
	|  \xF4&#x5B;\x80-\x8F]&#x5B;\x80-\xBF]{2}     # plane 16    
	)*$%xs', 
	$string );
} 
?&gt;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
