lets say there is an product list page, where product names and descriptions are listed. The description cant be longer than 20 characters. Its very easy to do in php... until I met the utf8 characters
here is a text from my native language: női dolog
but I cut it at the worst place, the ő character damages:
here is a text from my native language: n�
how to dodge it? I tried to utf encode and decode it, with less success. Ideas?
Use the encoding aware mb functions :
echo mb_substr($string, 0, 20, 'UTF-8');
For more information, read What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text .
You should use mb*
extension to work with multibyte strings. You can't use substr()
for them, because it works with bytes and not symbols (and they are not same in terms of multibyte encodings). So to trim string till 20 symbols, use mb_substr()
$result = mb_substr($data, 0, 20);//add UTF-8 to point utf encoding
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.