Re: PHP Tokenize a String by its length
- From: Lüpher Cypher <lupher.cypher@xxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 11:23:04 GMT
Geoff Berrow wrote:
Message-ID: <HWstf.630$i%4.305@trndny08> from Lüpher Cypher contained the following:
Shorter :)
function trancate($string,$length) { $string = substr(trim($string),0,$length); $string = substr($string,0,strrpos(trim($string)," ")); return "$string..."; }
You don't need the last trim()
$string = substr($string,0,strrpos($string," "));
Actually, it's needed :)
Suppose $string = "aaa bbb ccc" and $length = 9, then:
first substr leaves "aaa bbb "
if we don't have the second trim, strrpos returns 8 and second substr returns "aaa bbb " so, "aaa bbb ..." will be returned instead of "aaa bbb..." :)
luph .
- Follow-Ups:
- Re: PHP Tokenize a String by its length
- From: Geoff Berrow
- Re: PHP Tokenize a String by its length
- References:
- PHP Tokenize a String by its length
- From: Mavenos
- Re: PHP Tokenize a String by its length
- From: Geoff Berrow
- Re: PHP Tokenize a String by its length
- From: Lüpher Cypher
- Re: PHP Tokenize a String by its length
- From: Geoff Berrow
- PHP Tokenize a String by its length
- Prev by Date: Re: PHP Tokenize a String by its length
- Next by Date: Message box
- Previous by thread: Re: PHP Tokenize a String by its length
- Next by thread: Re: PHP Tokenize a String by its length
- Index(es):
Relevant Pages
|