|
Posted by Geoff Berrow on 10/23/05 11:26
I noticed that Message-ID: <0dB6f.4$jW4.3@fe24.usenetserver.com> from
David Haynes contained the following:
>
>$str50 = substr($mystring, 0, 50);
I use this method in a function and if the text is truncated the
function adds '...more' at the end. The function also takes a url so
that the word 'more' can be a link to an expanded version. $length is
obviously the amount you to which you want to truncate. The function
also 'steps back' to avoid truncating in the middle of a word.
<?php
function truncate($value,$length,$url){
if(strlen($value)>$length){
$value=substr($value,0,$length);
$n=0;
while(substr($value,-1)!=chr(32)){
$n++;
$value=substr($value,0,$length-$n);
}
$value=$value." ...<a href='$url'><em>more</em></a>";
}
return $value;
}
//usage
echo truncate("the quick brown fox jumps over the lazy
dog",15,"http:www.example.com?id=someid");
?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
[Back to original message]
|