{"id":24,"date":"2008-08-18T20:53:16","date_gmt":"2008-08-19T00:53:16","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=24"},"modified":"2008-08-18T20:53:16","modified_gmt":"2008-08-19T00:53:16","slug":"using-utf-8-in-php-gtk2","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2008\/08\/18\/using-utf-8-in-php-gtk2\/","title":{"rendered":"Using UTF-8 in PHP-GTK2"},"content":{"rendered":"<p><img decoding=\"async\" title=\"An image of PHP-GTK2 on Ubuntu trying to display Asian languages\" src=\"\/images\/labs\/ubuntu_phpgtk2_utf8_bad.png\" alt=\"An image of PHP-GTK2 on Ubuntu trying to display Asian languages\" align=\"right\" \/><br \/>\nWhen trying to use a string that has characters that are non-English (Asian and middle east characters) or characters that have accents for <a rel=\"nofollow\" href=\"http:\/\/gtk.php.net\/manual\/en\/gtk.gtkwidget.php\">PHP-GTK&#8217;s widgets<\/a>, you may possibly encounter a situation like the image to the right.<\/p>\n<p>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 <a rel=\"nofollow\" href=\"http:\/\/php.net\/utf8_encode\">utf8_encode<\/a>&#8216;ed strings to it. This also happens with non-english characters in an Gettext messages file.<\/p>\n<p>This can be accomplished with:<br \/>\n<code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">&lt;?php<br \/>\n<\/span><span style=\"color: #ff8000;\">\/\/\u00a0...\u00a0code<\/span><\/span><\/code><\/p>\n<p>\/\/\u00a0Instead\u00a0of\u00a0using\u00a0function($nonenglish),\u00a0use function(utf8_encode($nonenglish)) instead<br \/>\n<span style=\"color: #007700;\">echo(<\/span><span style=\"color: #0000bb;\">utf8_encode<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">file_get_contents<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$argv<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #0000bb;\">1<\/span><span style=\"color: #007700;\">])));<\/span><\/p>\n<p><span style=\"color: #ff8000;\">\/\/\u00a0&#8230;\u00a0code<br \/>\n<\/span><span style=\"color: #0000bb;\">?&gt;<br \/>\n<\/span><\/p>\n<p>The example above outputs the contents of the first parameter. You can use the above script to encode message.mo files with <strong>php <em>filename.php<\/em> message.mo &gt; message.mo<\/strong> &#8211; though you should consider making a backup.<\/p>\n<p>For displaying text inside <a rel=\"nofollow\" href=\"http:\/\/gtk.php.net\/manual\/en\/gtk.gtktextview.php\">GtkTextView<\/a> (set text via <a rel=\"nofollow\" href=\"http:\/\/gtk.php.net\/manual\/en\/gtk.gtktextbuffer.php\">GtkTextBuffer<\/a>) though, you will need to use <a rel=\"nofollow\" href=\"http:\/\/php.net\/iconv\">iconv<\/a> to convert the regional encoding to UTF-8:<\/p>\n<p><code><span style=\"color: #000000;\"><br \/>\n<span style=\"color: #0000bb;\">&lt;?php<br \/>\n<\/span><span style=\"color: #ff8000;\">\/\/\u00a0...\u00a0code<\/span><\/span><\/code><\/p>\n<p>\/\/\u00a0For\u00a0conversion\u00a0from\u00a0Windows\u00a0Notepad<br \/>\n<span style=\"color: #007700;\">echo <\/span><span style=\"color: #0000bb;\">iconv<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8220;windows-1252&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">&#8220;UTF-8&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">$str<\/span><span style=\"color: #007700;\">);<br \/>\n<\/span><span style=\"color: #ff8000;\">\/\/\u00a0For\u00a0conversion\u00a0from\u00a0French<br \/>\n<\/span><span style=\"color: #007700;\">echo <\/span><span style=\"color: #0000bb;\">iconv<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8220;IBM863&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">&#8220;UTF-8&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">$str<\/span><span style=\"color: #007700;\">);<br \/>\n<\/span><span style=\"color: #ff8000;\">\/\/\u00a0For\u00a0conversion\u00a0from\u00a0Chinese<br \/>\n<\/span><span style=\"color: #007700;\">echo <\/span><span style=\"color: #0000bb;\">iconv<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8220;GB2312&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #dd0000;\">&#8220;UTF-8&#8221;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">$str<\/span><span style=\"color: #007700;\">);<\/span><\/p>\n<p><span style=\"color: #ff8000;\">\/\/\u00a0&#8230;\u00a0code<br \/>\n<\/span><span style=\"color: #0000bb;\">?&gt;<br \/>\n<\/span><br \/>\nIf you found this article helpful, please help Compdigitec spread the word or please leave a comment. Don&#8217;t forget to <a href=\"\/labs\/feed\/\">subscribe<\/a> for more interesting articles!<\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[90,93,94,864,92,82,11,81,74,91,89],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/24"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}