<?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>big5 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/big5/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Wed, 04 Dec 2019 00:58:56 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>big5 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>PHP：UTF-8 跟 Big5 的糾結</title>
		<link>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/</link>
					<comments>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 17 Sep 2018 14:39:51 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[encodeURIComponent]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[big5]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[iconv]]></category>
		<category><![CDATA[亂碼]]></category>
		<category><![CDATA[rawurlencode]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=109</guid>

					<description><![CDATA[<p>今天在公司處理到一個編碼的問題，由於有許多歷史包袱都是以 big5 下去開發，短時間內沒有辦法把所有系統轉換成 utf-8 的情況下，就經常要面對編碼轉換的問題，這邊記錄一下今天遇到問題的解法。 有個&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/" data-wpel-link="internal">PHP：UTF-8 跟 Big5 的糾結</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>今天在公司處理到一個編碼的問題，由於有許多歷史包袱都是以 big5 下去開發，短時間內沒有辦法把所有系統轉換成 utf-8 的情況下，就經常要面對編碼轉換的問題，這邊記錄一下今天遇到問題的解法。</p>



<span id="more-109"></span>



<p>有個新的系統 B 必須要串接舊系統 A，B 是 utf-8 環境，A 是 big5 環境，目前設計是 B 透過 AJAX 來讀取 A 的資料，A 這邊吃的是經過 encodeURIComponent 處理過的資料，例如：「你好」 -&gt;&nbsp;「%A7%41%A6%6E」，前情提要到這邊。</p>



<p>小蛙的笨腦袋就開始想怎麼處理：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>該頁面拿到的資料都是 UTF-8，拿到之後用 iconv 轉 big5 先</li><li>轉成 big5 之後，再做 encodeURIComponent 就好了</li><li>easy ~</li></ul>



<p>整個串起來之後，正確的「你好」應該是「%A7%41%A6%6E」，小蛙卻得到「%EF%BF%BDA%EF%BF%BDn」，看起來好像差很大，於是想說可能是因為 php 要透過 javascript encodeURIComponent 來轉，雖然轉好 big5 但是又在 utf-8 的文本中印出導致的，於是開始往「一切都在 php 中轉好，javascript 直接拿到轉好值就丟出去」的方向進行，小蛙改變策略：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>在 php 中使用 iconv 把 UTF-8 轉成 big5</li><li>在 php 中使用 <a rel="noreferrer noopener nofollow external" href="https://blog.longwin.com.tw/2015/11/php-rawurlencode-urlencode-diff-2015/" target="_blank" data-wpel-link="external" class="wpel-icon-right">rawurlencode<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 轉換</li><li>把結果透過 javascript 去要資料</li></ul>



<p>嗯，看起來應該是無懈可擊！哦哦哦哦哦哦 ~ 高興的尖叫起來，隔壁同事都看了小蛙一眼，但回傳的結果怪怪的啊，看一下後台 log，「你好」變成了「%A7A%A6n」，額外測試一個「外部」卻變成了「%A5~%B3%A1」，花惹發？嗯 ~ 港節好像比第一次得到的更接近正解一點(?)，再來想想問題在哪，重新順一下流程好了，第一步跟第二步都做轉換的動作，其中一定有一個轉換失效了，要不第一個要不第二個(廢話)。</p>



<p>Google 了一下看到這篇「<a href="http://www.edbiji.com/doccenter/showdoc/4/nav/680.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">php 转码函数 你还在用iconv吗？<span class="wpel-icon wpel-image wpel-icon-6"></span></a>」，既然懷疑轉換出問題，那就把他換掉看看，事情絕對沒有憨人所想的那麼簡單，結果沒變！沒變 &#8230; 記得以前有一次使用 base64 來做過這件事，正當打算要來試的時候，超猛同事走了過來問說發生什麼事，小蛙大概跟他講解了一下，他只回了一句，那你把 iconv 轉的東西印出來看就知道了。</p>



<p>看來出現新的曙光，Google 查到這篇「<a href="https://stackoverflow.com/questions/14674834/php-convert-string-to-hex-and-hex-to-string" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">PHP convert string to hex and hex to string<span class="wpel-icon wpel-image wpel-icon-6"></span></a>」，於是把 iconv 改成&nbsp;mb_convert_encoding，然後再透過該文章中的 function 來做轉換，就可以成功轉換成系統 A 需要的格式了！</p>



<p>可喜可樂！留個備份，記錄超強同事的神蹟！</p>



<h2 class="wp-block-heading">發現問題 (2018-10-16更新)</h2>



<p>上面的作法用了一段時間後發現有些字串會解析成不一樣的東西，例如：<code>帶卜辭</code>實際上收到卻變成<code>帶尸辭</code>、<code>恆春</code>變成<code>恆柢</code>、<code>小屯</code>變成<code>匕屯</code>。<br>以「帶卜辭」來當作轉換的例子<br>帶 =&gt; 「%B1a」<br>卜 =&gt; 「%A4R」<br>辭 =&gt; 「%C3%E3」</p>



<p>如果用上面的方法轉出來，轉出來會是全小寫，<br>帶 &lt;= 「%b1a」<br>尸 &lt;= 「%a4r」<br>辭 &lt;= 「%c3%e3」</p>



<p>下方是把它改成全大寫<br>婢 &lt;= 「%B1A」<br>卜 &lt;= 「%A4R」<br>辭 &lt;= 「%C3%E3」</p>



<p>但以上兩者都不對，因為原本的方法轉出來之後，就通通變成小寫，而大小寫卻對應到不一樣的字，造成這次的問題。</p>



<h2 class="wp-block-heading">使用 rawurlencode (2018-10-16更新)</h2>



<p>只要先把要轉換的文字，正確地轉到 big5 之後，透過 rawurlencode 來做編碼就可以編出正確的內容囉！</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">// 先把要的文字轉成 big5，再整個透過 rawurlencode 轉換
$txt = rawurlencode(mb_convert_encoding($txt, "big5", "utf-8"));</pre>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/" data-wpel-link="internal">PHP：UTF-8 跟 Big5 的糾結</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/109/php%ef%bc%9autf-8-%e8%b7%9f-big5-%e7%9a%84%e7%b3%be%e7%b5%90/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
