[Project_owners] Unicode encoding (Hebrew, sinhala etc) using JAVASCRIPT & decoding using PHP

Roberto Bouzout (tito) zilla at nxuy.com
Sun Oct 26 22:23:09 PDT 2008


> I need to know how can I send an unicode text like Hebrew through
> JAVASCRIPT (encode) and get it from PHP (decode it) to save it to a
> MySQL database.

I do this:
	
	var url = 'http://www.domain.com/some/file?decoded='+X_EncodeUTF8('מחשבים');
	var browser = window.getBrowser();
	var tab = browser.addTab(url);

function X_EncodeUTF8(string)
{
	try
	{
		return encodeURIComponent(string);
	}
	catch(err)
	{
		try
		{
			return encodeURI(string);
		}
		catch(err)
		{
			return (string);
		}
	}
}

PHP:
echo $_GET['decoded'];

/*

will print 'מחשבים'
Assuming that your server 'serve' the file as UTF-8 ( see: "text/html; charset=UTF-"8 at the response 	headers)

*/

I do the try and catch in the X_EncodeUTF8 because sometimes the argument maybe is malformed.. but valid.

alert(decodeURI('http://example.net/canci%f3n/'))

Error: URIError: malformed URI sequence
Source file: Javascript Command
Line: 1



More information about the Project_owners mailing list