Compdigitec Labs

« | Home | »

Using UTF-8 in PHP-GTK2

By admin | August 18, 2008

An image of PHP-GTK2 on Ubuntu trying to display Asian languages
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 | 7 Comments »

7 Responses to “Using UTF-8 in PHP-GTK2”

  1. boat rentals near fort lauderdale Says:
    November 2nd, 2024 at 22:16

    … [Trackback]

    […] There you can find 91076 additional Info to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  2. โรงงานผลิตสายรัดพลาสติก Says:
    November 6th, 2024 at 23:10

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  3. fenix168 Says:
    November 12th, 2024 at 17:08

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  4. BAU4IQ Says:
    November 16th, 2024 at 07:36

    … [Trackback]

    […] Read More here to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  5. Jaxx Liberty Says:
    November 18th, 2024 at 21:04

    … [Trackback]

    […] Find More on to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  6. 入金不要ボーナス Says:
    November 27th, 2024 at 03:52

    … [Trackback]

    […] Here you can find 89065 more Info to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

  7. หวยดาวโจนส์ Says:
    December 1st, 2024 at 19:20

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2008/08/18/using-utf-8-in-php-gtk2/ […]

Comments