简体   繁体   中英

Strlen for non english language

i want to display short description of articles on home page. Descriptions are a mix of Thai and English language.

I am using this function for strlen

mb_strlen($str, 'UTF-8');

but this is not acurate as some descriptions end up in just one line and some goes upto 3 lines and I want to show descriptions of two lines.

If strlen is bigger than 155 i do

$descr = mb_strlen($descr, 'UTF-8') > 155 ? substr($descr, 0, 152) . '...' : $descr;

Thank You.

Glyphs , the graphical representations of characters, have different widths in different fonts. Just compare the m with i :

mmmmmmmmmm
iiiiiiiiii

Both characters are repeated ten times. But the glyph of the m is much broader than the glyph of the i .

So you cannot conclude the width of its graphical representation from the number of characters (except for monospaced fonts).

substr is unsafe to use on utf-8 data. Use mb_substr

如果要防止输入3行或更多行的条目,请先用'\\n'分隔字符串,然后使用mb_substr

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM