tags:

views:

69

answers:

3

Hello,

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.

+4  A: 

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).

Gumbo
So... how would you do it? :)
Adam Kiss
+1 for correctly (I think) interpreting an unclear question :)
Nick Meyer
Thats not a big deal I understand this but its causing the difference not because of widths but the strlen differences in languages
Shishant
@Shishant: Can you give an example?
Gumbo
Shishant
@Shishant: So the first example is just *displayed* in three line?
Gumbo
@gumbo/@shrishant: My understanding is that its we are talking about web application. So if its web page will it be better if entire text is pushed to page and have JavaScript and CSS to do rest of the work ?
Anil Namde
@Gumbo Yes its display in 3 line and one line after truncating.
Shishant
@Shishant: Well, as I said: Different glyphs have different dimension. You would need to know the actual glyphs and its dimensions to get exactly just one line. The best is to do this on the client side with JavaScript.
Gumbo
@Shishant: Take a look at this: http://adamhooper.com/bodacity/playground/jquery.excerpt.html
Gumbo
+1  A: 

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

troelskn
Thank You I use it
Shishant
A: 

if you want prevent entries with 3 ore more lines firstly split string by '\n' and then make triming with mb_substr

shuvalov