Re: PHP Tokenize a String by its length
- From: Geoff Berrow <blthecat@xxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 10:10:38 +0000
Message-ID: <1136022722.493964.17770@xxxxxxxxxxxxxxxxxxxxxxxxxxxx> from
Mavenos contained the following:
>I mean if I have a 2 page content - just I want to display only 4 lines
>having 200 letters then follows the content with "....." at the end of
>short pera.
>
>Can anybody help in this regards,
>Thanks in advance
I wrote this function to do just that. It has the added feature that it
truncates on a space rather than the middle of the word.
$string is the long string to truncate
$length is the maximum truncated length
$url is the URL you should go to to see the whole paragraph
<?php
function truncate($string,$length,$url){
$string=trim($string);
if(strlen($string)>$length){
$string=substr($string,0,$length);
$n=0;
while(substr($string,-1)!=chr(32)){
$n++;
$string=substr($string,0,$length-$n);
}
$string=trim($string)."... <a href='$url'><em>more</em></a>";
}
return $string;
}
//example usage
echo truncate("the quick brown fox jumps over the lazy
dog",35,"anyurl");
?>
--
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/
.
- Follow-Ups:
- Re: PHP Tokenize a String by its length
- From: Lüpher Cypher
- Re: PHP Tokenize a String by its length
- References:
- PHP Tokenize a String by its length
- From: Mavenos
- PHP Tokenize a String by its length
- Prev by Date: PHP Tokenize a String by its length
- Next by Date: Re: PHP Tokenize a String by its length
- Previous by thread: PHP Tokenize a String by its length
- Next by thread: Re: PHP Tokenize a String by its length
- Index(es):
Relevant Pages
|