<?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>答客問 &#187; UTF-8</title>
	<atom:link href="http://www.qna.tw/tag/utf-8/feed" rel="self" type="application/rss+xml" />
	<link>http://www.qna.tw</link>
	<description>網路疑難情報指南 Question and Answer over Internet</description>
	<lastBuildDate>Wed, 21 Dec 2011 10:56:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>用 php 把 unicode 轉成 UTF-8 的最簡方案</title>
		<link>http://www.qna.tw/php-codelet-for-unicode-utf8-transfer</link>
		<comments>http://www.qna.tw/php-codelet-for-unicode-utf8-transfer#comments</comments>
		<pubDate>Thu, 06 Aug 2009 13:52:22 +0000</pubDate>
		<dc:creator>管理員</dc:creator>
				<category><![CDATA[網路技術]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://2009.qna.tw/?p=52</guid>
		<description><![CDATA[越來越多資訊包含了多種語言，以往單純的 Big5 文件已經無法處理，所以大家漸漸傾向把文件輸出成 unicode 型式。然而，Unicode 不像 Big5 只是單純的雙位元字集，佔用固定的 code range 那樣單純，它的複雜度更高。 在尋找適合的 solutions 時，發現以 Java, C 此類語言的資源較為豐富，而 php 則是要到 php 6 之後才會內建，而且要編譯 International Components for Unicode 的相關模組。而現在大家慣用的 iconv 指令，卻無法在 unicode 的編碼方式作切換。（會的人麻煩 comment 指點一下喔，謝謝！） 花了一點時間爬文後，整理出以下的最簡方案： Unicode 轉 UTF-8 首先，檢查檔案是否包含 Unicode BOM 標頭(\xFF\xFE)。 然後，兩兩取出字節，轉換成 binary string。 轉換過程中，利用 $cache 雜湊表來節省不必要的運算；而 ASCII 區段的文字則另外經過 iconv 的轉換以相容部份特殊符號。（以下字碼轉換的程式參考自 CentralNic Unicode Library，但修正了原碼中對 $dec &#60; 128 [...]]]></description>
			<content:encoded><![CDATA[<p>越來越多資訊包含了多種語言，以往單純的 Big5 文件已經無法處理，所以大家漸漸傾向把文件輸出成 unicode 型式。然而，Unicode 不像 Big5 只是單純的雙位元字集，佔用固定的 code range 那樣單純，它的複雜度更高。</p>
<p>在尋找適合的 solutions 時，發現以 Java, C 此類語言的資源較為豐富，而 php 則是要到 php 6 之後才會內建，而且要編譯 <a href="http://www.icu-project.org/" target="_blank">International Components for Unicode</a> 的相關模組。而現在大家慣用的 iconv 指令，卻無法在 unicode 的編碼方式作切換。（會的人麻煩 comment 指點一下喔，謝謝！）</p>
<p>花了一點時間爬文後，整理出以下的最簡方案：<span id="more-52"></span><br />
<span style="font-weight: bold;"><span style="color: #0000ff;">Unicode 轉 UTF-8</span></span><br />
首先，檢查檔案是否包含 Unicode BOM 標頭(\xFF\xFE)。<br />
然後，兩兩取出字節，轉換成 binary string。<br />
轉換過程中，利用 $cache 雜湊表來節省不必要的運算；而 ASCII 區段的文字則另外經過 iconv 的轉換以相容部份特殊符號。（以下字碼轉換的程式參考自 <a href="http://labs.centralnic.com/Unicode.php" target="_blank">CentralNic Unicode Library</a>，但修正了原碼中對 $dec &lt; 128 的設定，將之擴大為 256 以改善其特殊符號不相容的問題。）<br />
程式碼如下：</p>
<pre>function unicode_to_utf8($text='') {
	$cache	=	array() ;
	$text	=	preg_replace("/^\\xFF\\xFE/","",$text) ;
	for	($i = 0 ; $i &lt; strlen($text) ;) {
		$dec	=	ord(substr($text,$i + 1,1)) * 256 + ord(substr($text,$i,1)) ;
		$i	+=	2 ;
		if	(!array_key_exists($dec,$cache))	{
			if	($dec &lt; 256)	$cache[$dec]	=	iconv('ISO-8859-1','UTF-8',chr($dec)) ;
			else if ($dec &lt; 2048)	$cache[$dec]	=	chr(192 + (($dec - ($dec % 64)) / 64)) . chr(128 + ($dec % 64)) ;
			else			$cache[$dec]	=	chr(224 + (($dec - ($dec % 4096)) / 4096)) . chr(128 + ((($dec % 4096) - ($dec % 64)) / 64)) . chr(128 + ($dec % 64)) ;
		}
		$string	.=	$cache[$dec] ;
	}
	return	$string ;
}</pre>
    ]]></content:encoded>
			<wfw:commentRss>http://www.qna.tw/php-codelet-for-unicode-utf8-transfer/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

