homepage Welcome to WebmasterWorld Guest from 23.22.173.58
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
cutting off string with character limits
whatson

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4511030 posted 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?

 

swa66

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



 
Msg#: 4511030 posted 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.

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

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



 
Msg#: 4511030 posted 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

Was thinking CSS too, or some javascript.

httpwebwitch

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



 
Msg#: 4511030 posted 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.

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

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



 
Msg#: 4511030 posted 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved