Javascript / php - truncate a string to fit on a specific number of lines

I have a string that needs to be inserted into a field and should be no more than 3 lines long. To shorten it, I plan to truncate it and end it with "...". I could shorten it to a certain number of characters, but if I make it look good with "wwwwwwwww [...] wwww" it won't look right with "iiiiiiiiiii [...] iiii".

Is there a way to shorten it by how much space the line will take as opposed to the number of characters per line without using a fixed-width font? Ideally I would like to do this on the server side (php), but admit that the actual character width stuff is much more likely to be possible on the client side (JS / jQuery)

Mala

ps. Please "just create an image" ... "and put it on the end of the line" hacks or similar ", I really want to shorten the line to the appropriate length

+2


a source to share


5 answers


There's imageftbbox and imagettfbbox for that.



+2


a source


take a look at this: http://www.switchonthecode.com/tutorials/javascript-tutorial-how-to-auto-ellipse-text



+2


a source


This site offers as good a way as any and is cross-browser. I like the simplicity:

+1


a source


It is not possible that I can easily get the font size to the extent you are talking about, but there might be a hack that "appears" the way you want it. My guess is that since you mention three lines, we're talking about an element <textarea>

:

<textarea rows="3" cols="50" style="overflow:hidden;"></textarea>

      

Although there is text, it is not displayed and the scroll bars are not displayed.

0


a source


There are several jQuery plugins for this: http://plugins.jquery.com/taxonomy/term/2229

0


a source







All Articles