Saturday, June 18, 2011

PHP cut string properly

Common problem with PHP development is when you cut a non-latin string, it has strange symol at the end, looking like "�".

It's all because string is in Unicode format(mainly utf-8), however a substr function only works with single-byte encodings.

Instead of using substr, you must use mb_substr.

substr($string, $length, $count)
replace to
mb_substr($string, $length, $count, 'utf-8')

No comments:

Post a Comment