PHP – Convert Big5 to UTF-8

I am not familiar with the character encoding. I have a ISO-8859-1 string(Big5). After i convert it to UTF-8 by the utf8_encode() function, garbage characters are shown in the HTML. Finally, i found another way to convert Big5 to UTF-8.

The following function will break down a Big5 string into characters and convert them into UTF-8 one by one.

function big52utf8($big5str) {

	$blen = strlen($big5str);
	$utf8str = "";

	for($i=0; $i<$blen; $i++) {
		$sbit = ord(substr($big5str, $i, 1));
		//echo $sbit;
		//echo "<br>";
		if ($sbit < 129) {
			$utf8str.=substr($big5str,$i,1);
		} elseif ($sbit > 128 && $sbit < 255) {
			$new_word = iconv("BIG5", "UTF-8", substr($big5str,$i,2));
			$utf8str.=($new_word=="")?"?":$new_word;
			$i++;
		}
	}

	return $utf8str;

}

 

Done =)

Reference: 台灣PHP聯盟[ Taiwan PHP User Group ] – php iconv Big5 to UTF-8

3 thoughts on “PHP – Convert Big5 to UTF-8”

  1. The above function is not converting BIG5 to UTF-8. Please help me..
    Is there any language package need to be installed in my system?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.