Page 1 of 1

I cannot [b]encode url [/b]strings for using with flash.

Posted: Sun Aug 04, 2002 7:29 am
by mongkol
Firstly, I used "urlencode()" command on php to encode my string which is "มนต์รักเพลงสการ์" <-- Thai language..
It returned "%C1%B9%B5%EC%C3%D1%A1%E0%BE%C5%A7%CA%A1%D2%C3%EC" ... I try to use it with flash but I cannot..

BUT... when I posted the same string(thai language) on goggle search engine. It returned my string which is already encoded on URL...
It returned "%E0%B8%A1%E0%B8%99%E0%B8%95%E0%B9%8C%E0%B8%A3%E0%B8%B1%E0%B8%81%E0%B9%80%E0%B8%9E%E0%B8%A5%E0%B8%87%E0%B8%AA%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%8C" Moreover, this string can use with my flash properly....

So, I would generate string which use the same algorithm with google.. Has anyone know what I can do? ...

Thank you in advance....
mongkol

Posted: Sun Aug 04, 2002 5:05 pm
by josa
The string from Google was probably converted to utf-8 before it was coded. Since your string is in unicode and not ISO-8859-1 you can't use the built in function utf8_encode(), but I found this under the User Contributed Notes in the PHP manual:
ronen@greyzone.com
07-Mar-2002 12:01

The following function will utf-8 encode unicode entities &#nnn(nn);
with n={0..9}

Code: Select all

/**
* takes a string of unicode entities and converts it to a utf-8 encoded
string
* each unicode entitiy has the form &#nnn(nn); n=&#123;0..9&#125; and can be
displayed by utf-8 supporting
* browsers.  Ascii will not be modified.
* @param $source string of unicode entities &#1111;STRING]
* @return a utf-8 encoded string &#1111;STRING]
* @access public
*/
function utf8Encode ($source) &#123;
	$utf8Str = '';
	$entityArray = explode ("&#", $source);
	$size = count ($entityArray);
	for ($i = 0; $i < $size; $i++) &#123;
		$subStr = $entityArray&#1111;$i];
		$nonEntity = strstr ($subStr, ';');
		if ($nonEntity !== false) &#123;
			$unicode = intval (substr ($subStr, 0, (strpos ($subStr, ';') + 1)));
			// determine how many chars are needed to reprsent this unicode char
			if ($unicode < 128) &#123;
				$utf8Substring = chr ($unicode);
			&#125;
			else if ($unicode >= 128 && $unicode < 2048) &#123;
				$binVal = str_pad (decbin ($unicode), 11, "0",STR_PAD_LEFT);
				$binPart1 = substr ($binVal, 0, 5);
				$binPart2 = substr ($binVal, 5);
			
				$char1 = chr (192 + bindec ($binPart1));
				$char2 = chr (128 + bindec ($binPart2));
				$utf8Substring = $char1 . $char2;
			&#125;
			else if ($unicode >= 2048 && $unicode < 65536) &#123;
				$binVal = str_pad (decbin ($unicode), 16, "0",STR_PAD_LEFT);
				$binPart1 = substr ($binVal, 0, 4);
				$binPart2 = substr ($binVal, 4, 6);
				$binPart3 = substr ($binVal, 10);
			
				$char1 = chr (224 + bindec ($binPart1));
				$char2 = chr (128 + bindec ($binPart2));
				$char3 = chr (128 + bindec ($binPart3));
				$utf8Substring = $char1 . $char2 . $char3;
			&#125;
			else &#123;
				$binVal = str_pad (decbin ($unicode), 21, "0",STR_PAD_LEFT);
				$binPart1 = substr ($binVal, 0, 3);
				$binPart2 = substr ($binVal, 3, 6);
				$binPart3 = substr ($binVal, 9, 6);
				$binPart4 = substr ($binVal, 15);
		
				$char1 = chr (240 + bindec ($binPart1));
				$char2 = chr (128 + bindec ($binPart2));
				$char3 = chr (128 + bindec ($binPart3));
				$char4 = chr (128 + bindec ($binPart4));
				$utf8Substring = $char1 . $char2 . $char3 . $char4;
			&#125;
			
			if (strlen ($nonEntity) > 1)
				$nonEntity = substr ($nonEntity, 1); // chop the first char (';')
			else 
				$nonEntity = '';

			$utf8Str .= $utf8Substring . $nonEntity;
		&#125;
		else &#123;
			$utf8Str .= $subStr;
		&#125;
	&#125;

	return $utf8Str;
&#125;
Ronen.
Use this function to utf8 encode your string before you send it to urlencode() like this:

Code: Select all

urlencode(utf8Encode("&#3617;&#3609;&#3605;&#3660;&#3619;&#3633;&#3585;&#3648;&#3614;&#3621;&#3591;&#3626;&#3585;&#3634;&#3619;&#3660;"));
/josa

thank you a lot.. It works.

Posted: Mon Aug 05, 2002 1:20 am
by mongkol
Thank you. :D