Monday, November 4, 2013

[PHP][solved] fix garbled text problem when using substr() operation.

For my case, i need use the substr() function is cut the a part of sentence, but get some garbled text.
The garbled text are shown as the image below.

And here is the function i used:
$blog_content = substr($article['post_title'],0,10);
------------------------------------------------------------------------
To fix the problem , you can use mb_substr() function to performs a multi-byte safe substr() operation.
Position is counted from the beginning of str. First character's position is 0. Second character position is 1, and so on.
$blog_content = mb_substr($article['post_title'],0,10,'utf8');

The encoding parameter is the character encoding, for my case, i would like to use 'utf8'.
And this is the result:



No comments :

Post a Comment