<?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>gbk &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/gbk/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 31 Dec 2012 21:47:49 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>PHP处理GBK转换为UTF8字符集编码时报错</title>
		<link>https://www.mudbest.com/php%e5%a4%84%e7%90%86gbk%e8%bd%ac%e6%8d%a2%e4%b8%bautf8%e5%ad%97%e7%ac%a6%e9%9b%86%e7%bc%96%e7%a0%81%e6%97%b6%e6%8a%a5%e9%94%99/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 31 Dec 2012 21:47:49 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gbk]]></category>
		<category><![CDATA[iconv]]></category>
		<category><![CDATA[编码转换]]></category>
		<category><![CDATA[错误]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1743</guid>

					<description><![CDATA[函数介绍： iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件。例如：从 ... <a title="PHP处理GBK转换为UTF8字符集编码时报错" class="read-more" href="https://www.mudbest.com/php%e5%a4%84%e7%90%86gbk%e8%bd%ac%e6%8d%a2%e4%b8%bautf8%e5%ad%97%e7%ac%a6%e9%9b%86%e7%bc%96%e7%a0%81%e6%97%b6%e6%8a%a5%e9%94%99/" aria-label="阅读 PHP处理GBK转换为UTF8字符集编码时报错">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p><strong>函数介绍：</strong></p>
<p><strong>iconv()介绍</strong><br />
iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件。例如：从GB2312转换为UTF-8。<br />
iconv函数在php5中内置，GB字符集默认打开。</p>
<p><strong>iconv()错误</strong><br />
iconv在转换字符”—”到gb2312时会出错，解决方法是在需要转成的编码后加 “//IGNORE”，也就是iconv函数第二个参数后。如下：<br />
iconv(&#8220;UTF-8&#8221;, &#8220;GB2312//IGNORE&#8221;, $data)<br />
ignore的意思是忽略转换时的错误，如果没有ignore参数，所有该字符后面的字符串都无法被保存。<span id="more-1743"></span></p>
<p>iconv()例子</p>
<p><strong>例1：</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
echo $str= ‘你好,这里是卖咖啡!’;
echo '&lt;br /&gt;';
echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8
echo '&lt;br /&gt;';
echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符个数截取而非字节
print_r(iconv_get_encoding()); //得到当前页面编码信息
echo iconv_strlen($str, 'UTF-8'); //得到设定编码的字符串长度
?&gt;
</pre>
<p><strong>例2：</strong><br />
如果你的PHP文件是UTF-8编码的，那么下面的代码就可以正确输出：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$str='我爱百度';
$utf='';
for ($i=0;$i&lt;strlen($str);$i++) $utf.=sprintf(&quot;%%%02X&quot;,ord(substr($str,$i,1)));
echo $utf;
?&gt;
</pre>
<p>如果你的PHP文件是GB的，那么下面的代码可以工作：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$str='我爱百度';
$str=iconv(&quot;GBK&quot;, &quot;UTF-8&quot;, $str);
$utf='';
for ($i=0;$i&lt;strlen($str);$i++) $utf.=sprintf(&quot;%%%02X&quot;,ord(substr($str,$i,1)));
echo $utf;
?&gt;
</pre>
<p>以上情况下，程序输出的内容是：</p>
<pre class="brush: plain; title: ; notranslate">
%E6%88%91%E7%88%B1%E7%99%BE%E5%BA%A6
</pre>
]]></content:encoded>
					
		
		
			</item>
		<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是三字节，可以根据 中文的范围来判断 // 返回: true -  ... <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>
	</channel>
</rss>
