php utf8 text doesnt render correctly
I have a table where i have a TEXT column in utf8_unicode_ci format.
I also have a form where i can put multi language text into this field.
When i fetch each text from this column i want to show only 50 characters in length so i do the following:
$text = $rows["text"];
$text = mb_substr($text, 0, 50);
$text = htmlentities($text, ENT_COMPAT, 'UTF-8');
this fails to render non english characters and doesnt show anything, if i put mb_substr function below htmlentities it will show up but it will cut the text and show some &letter;&another;
any wa开发者_如何学Goy i can solve this issue?
You can use mb_internal_encoding to change the char encoding to UTF-8, that might solve it.
mb_internal_encoding('UTF-8');
If you're on mySQL < 5.0.7:
Enable utf -8 while querying using mysql_query("SET NAMES utf8;");
if you're on a newer mySQL:
mysql_set_charset("utf8");
AND
header('Content-type: text/plain;charset=UTF-8');
精彩评论