Hey, there's a string I'm taking from a database and displaying. I would like to shorten that string if it is longer than a certain number of characters and add "..." at the end. How would I do this? Thanks.
Hey, there's a string I'm taking from a database and displaying. I would like to shorten that string if it is longer than a certain number of characters and add "..." at the end. How would I do this? Thanks.
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
Check out the following thread:
http://www.dynamicdrive.com/forums/s...ghlight=substr
Hope this helps.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
Better would be a soft limit in words, then a hard limit in characters:Code:function truncate_passage($tr, $wordlimit = 20, $charlimit = 0, $tail = '...') { $utail = false; if($wordlimit) { $tr = explode(' ', $tr); $utail = count($tr) > $wordlimit; $tr = implode(' ', array_slice($tr, 0, $wordlimit)); } if($charlimit) { $utail = $utail || strlen($tr) > $charlimit; $tr = substr($tr, 0, $charlimit); } if($utail) $tr .= $tail; return $tr; }
Last edited by Twey; 12-04-2007 at 04:45 PM. Reason: Typo fix.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
I think I may have a use for both. Thanks guys![]()
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
only question I have is what is "utail" ?
Edit: BEGIN
that script allows both.Originally Posted by alexjewell
truncate_passage($tr, $wordlimit = 20, $charlimit = 0, $tail = '...')
Edit: END
Last edited by boogyman; 12-04-2007 at 02:06 PM. Reason: added response
alexjewell, there's nothing that does that mine doesn't.
boogyman, whether to append the tail or not. I called it $tail, then made it into a function, and $tail was the logical name for the fourth parameter, so I had to rename it.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
I used substr and strlen:
Code:if(strlen($row['Title'])){ $row['Title'] = substr($row['Title'],0,25).'...';}
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
that will be fine if this is the one and only time you will EVER need to do this, including not only in this script but forever, which the liklihood of you encountering something else you would like to limit is very very slim.
Twey was giving you a generic fuction that you could re-use over and over and over. and cover a number of parameters and options.
but we aren't here to dictate how you write your site, so I'm happy you found the solution. a good reference is www.php.net which is the homesite of the programming language, and it is very well documented.
Twey, I thought it was just a check to see whether or not to include the tail but what shook me was
$utail = count($tr) > $wordlimit;That first one is the one that threw me off because its setting the tail by default where the second is creating a check first?$utail = $utail || strlen($tr) > $charlimit;
The main problem with this is that it's liable to cut off the string halfway through a word: "I welcome you all here tod" while mine will try not to do so where possible: "I welcome you all here today..." That was the main problem I was trying to solve. The hard character limit is off by default but should probably be set too, since otherwise one very long word wouldn't get truncated.I used substr and strlen:What? The former is equivalent to:That first one is the one that threw me off because its setting the tail by default where the second is creating a check first?And the latter:Code:if(count($tr) > $wordlimit) // if there are more words than allowed $utail = true;Without the check for $utail, the function wouldn't append the dots if the string were truncated by words and then was shorter than the character limit.Code:if($utail || strlen($tr) > $charlimit) $utail = true;
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Another thought--
PHP Code:function cut_it($str,$len=20) { //default is 20, but you can switch to anything you want here on when calling it
if (strlen($str)<=$len) { return $str; }
$str = wordwrap($str,$len); //defaults to the behaviour twey setup
$str = explode("\n",$str,2); //lazy-- you could use substr/strpos, etc
return $str[0];
}
$test = 'whatever and stuff this is some text etc';
$test = cut_it($test,15);
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks