Pages

Showing posts with label utf8. Show all posts
Showing posts with label utf8. Show all posts

Tuesday, November 30, 2010

PHP substring with UTF-8

You may have noticed strange characters (like �) to appear when you try using the PHP substr function with a UTF8 string. The solution is easy, elegant and core PHP: mb_substr

For example mb_substr("Greek χαρακτήρες",0,10,"UTF8"), will return the first 10 UTF8 characters of our UTF8 string.

For more details see the PHP mb_substr manual.

Thursday, November 18, 2010

mySQL and UTF-8 character display using PHP

Many developers face character rendering issues when fetching UTF-8 stings from a mySQL database using PHP. Even though the page character encoding is set to UTF-8 and the database collation is also set to utf8_general_ci.

For me the problem was resolved by sending the following query from PHP:
 mysql_query("SET NAMES utf8");

just after using mysql_connect and mysql_select_db PHP functions.