Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

cutting off string with character limits

     

whatson

9:55 pm on Oct 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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?

swa66

10:25 pm on Oct 22, 2012 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Ref: [php.net...]
Ref: [php.net...]
Ref: [php.net...]


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.

Ref: [php.net...]
Ref: [php.net...]

Or you could find the fisrt occurance of a space in the second part andthen add the relevant part back to the first part.

Ref: [php.net...]
Ref: [php.net...]


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)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



>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

Was thinking CSS too, or some javascript.

httpwebwitch

5:30 pm on Oct 23, 2012 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

There is a way to do it - with JavaScript. render a copy of the string on the page, and hide it from view, possibly with visibility:hidden or position:absolute;left:5000. Make sure it has the same CSS styling as it'll have in your real layout. Don't use display:none, because then it'll have no width. Measure the width. If it's too wide, chop a letter off the end (or a word, split on whitespace boundaries), then repeat. Then when the string is fitting inside your pixel dimensions, replace the visible text with the hidden text.

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.

httpwebwitch

5:32 pm on Oct 23, 2012 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month