Re: PHP Tokenize a String by its length



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 .



Relevant Pages

  • Re: PHP Tokenize a String by its length
    ... >first substr leaves "aaa bbb " ... >if we don't have the second trim, strrpos returns 8 and second substr ... Prev by Date: ...
    (comp.lang.php)
  • Re: PHP Tokenize a String by its length
    ... Geoff Berrow wrote: ... first substr leaves "aaa bbb " ... if we don't have the second trim, strrpos returns 8 and second substr ... it'll become "aaa bbb" and strrpos will ...
    (comp.lang.php)