|cutting off string with character limits|
| 9:55 pm on Oct 22, 2012 (gmt 0)|
e.g. you have a page all about widgets, with a big description.
There is also a directory page, which lists results of all the widget pages, with their names hyperlinked, and a brief description.
Now, in the results page you want to produce some brief descriptions from the big description.
e.g. you want to limit the characters to 200.
1. How do you obtain the first 200 characters of a string?
2. Does the limit have to be characters? i.e. can it be pixels?
3. I don't want to cut a word off half way through, do you have to include some sort if statement, that if the next character is not a space?
4. How do you cut off a string at the period.
5. How do you cut off the string at the end of a paragraph?
| 10:25 pm on Oct 22, 2012 (gmt 0)|
1 the function strstr could be used to split it in a first and second part, where the first is 200 chars long. Take care if you have a multibyte encoding like UTF-8, then you need mb_substr!
There's also mb_strimwidth, which gets even closer to what you seek.
2. It's VERY difficult to predict how all browsers will convert characters into pixels. In essence: you have no chance. The only thing you could do is count on a hidden overflow in CSS, add in some fading/scrolling etc. and you can get the best of both worlds there,
But the server knwoing how long it will be in pixels: forget it. At best you might get a clue, but that requires knowledge of what font will chosen by the browser and how wide every character will be.
3. You could search for the last space in the first substring and then shorten it relative to the found position.
Or you could find the fisrt occurance of a space in the second part andthen add the relevant part back to the first part.
Again: the mb- varaints for for multi byte encodings such as UTF-8 UTF-16 etc ...
4. Once you know how to cut off at spaces, the periods become easy :-0
5. It's the same agin, but you need to first define how you encode paragraphs :-)
|brotherhood of LAN|
| 10:42 pm on Oct 22, 2012 (gmt 0)|
>2. It's VERY difficult to predict how all browsers will convert characters into pixels. In essence: you have no chance. The only thing you could do is count on a hidden overflow in CSS
| 5:30 pm on Oct 23, 2012 (gmt 0)|
There are some decent functions out there that will truncate a string on a word boundary. Those are helpful.
it's not impossible to truncate based on pixels.
I say it's possible but really you don't want to do that. It's nasty, and will do weird things on your page because the truncation of long strings can take a second or more. A couple of years ago I built a MooTools class that did pixel-perfect truncation, and tried it out on our website... the effect was so awful we trashed it and went with a simple character limit instead.
| 5:32 pm on Oct 23, 2012 (gmt 0)|
a container with overflow:hidden is ok. It might slice a letter in half where the string overflows the container boundary, which looks especially odd if the font is large.