Reply to Re: PHP Tokenize a String by its length

Your name:

Reply:


Posted by Lόpher Cypher on 12/31/05 12:19

Geoff Berrow wrote:
> Message-ID: <1136022722.493964.17770@g49g2000cwa.googlegroups.com> 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");
> ?>
>

Shorter :)

function trancate($string,$length) {
$string = substr(trim($string),0,$length);
$string = substr($string,0,strrpos(trim($string)," "));
return "$string...";
}

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация