« Changing the application name of a GtkAboutDialog in PHP-GTK2 | Home | Compiling re2c 0.13.5 on Ubuntu 8.04+ (binaries available) »
Using UTF-8 in PHP-GTK2
By admin | August 18, 2008
When trying to use a string that has characters that are non-English (Asian and middle east characters) or characters that have accents for PHP-GTK’s widgets, you may possibly encounter a situation like the image to the right.
This bug or glitch is caused by the fact that you are trying to use a regional encoding for the application, when in fact you should be passing utf8_encode‘ed strings to it. This also happens with non-english characters in an Gettext messages file.
This can be accomplished with:
<?php
// ... code
// Instead of using function($nonenglish), use function(utf8_encode($nonenglish)) instead
echo(utf8_encode(file_get_contents($argv[1])));
// … code
?>
The example above outputs the contents of the first parameter. You can use the above script to encode message.mo files with php filename.php message.mo > message.mo – though you should consider making a backup.
For displaying text inside GtkTextView (set text via GtkTextBuffer) though, you will need to use iconv to convert the regional encoding to UTF-8:
<?php
// ... code
// For conversion from Windows Notepad
echo iconv(“windows-1252”,“UTF-8”,$str);
// For conversion from French
echo iconv(“IBM863”,“UTF-8”,$str);
// For conversion from Chinese
echo iconv(“GB2312”,“UTF-8”,$str);
// … code
?>
If you found this article helpful, please help Compdigitec spread the word or please leave a comment. Don’t forget to subscribe for more interesting articles!
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: PHP | 16 Comments »
February 7th, 2022 at 06:38
… [Trackback]
[…] Here you can find 50056 additional Information on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
April 22nd, 2022 at 01:35
… [Trackback]
[…] Read More Information here on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
April 22nd, 2022 at 03:38
… [Trackback]
[…] Read More on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
April 25th, 2022 at 04:39
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
May 31st, 2022 at 17:12
… [Trackback]
[…] Find More here to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
July 22nd, 2022 at 13:03
… [Trackback]
[…] Read More Info here to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
August 11th, 2022 at 07:46
… [Trackback]
[…] Find More here on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
August 19th, 2022 at 18:28
… [Trackback]
[…] Find More Information here on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
August 31st, 2022 at 12:57
… [Trackback]
[…] Read More on to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
September 24th, 2022 at 13:06
… [Trackback]
[…] Info on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
October 5th, 2022 at 20:53
… [Trackback]
[…] Read More Info here to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
November 25th, 2022 at 18:10
… [Trackback]
[…] Information on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
January 6th, 2023 at 01:20
… [Trackback]
[…] Read More on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
January 24th, 2023 at 06:39
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
February 3rd, 2023 at 18:38
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]
March 5th, 2023 at 08:37
… [Trackback]
[…] Info on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]