Re: PHP Tokenize a String by its length



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/
.



Relevant Pages

  • Re: PHP Tokenize a String by its length
    ... at the end of short pera. ... Can anybody help in this regards, ... truncates on a space rather than the middle of the word. ... $string is the long string to truncate ...
    (comp.lang.php)
  • Re: nvarchar parameter not truncated based on size of field
    ... and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook) ... .NETCF 1.x - Truncates parameter automatically ... "William Vaughn" wrote: ... check the size of each string before assigning? ...
    (microsoft.public.sqlserver.ce)
  • Re: nvarchar parameter not truncated based on size of field
    ... Bill, Thanks for the info. ... It's still kind of bothering me that this functionality has changed since ... ..NETCF 1.x - Truncates parameter automatically ... check the size of each string before assigning? ...
    (microsoft.public.sqlserver.ce)
  • Re: sed issue
    ... the log file, it truncates the file after the first line, and I have been ... unable to get it to read past the last character of the first line. ... 0D, or to be more exact, the string with which sed seems to be having ... To get rid of control chars leaving only tabs and newlines, ...
    (comp.unix.shell)
  • Re: sed issue
    ... the log file, it truncates the file after the first line, and I have been ... This seems to be the only instance of the problematic string, but it is in every one of the expect log output files. ... How can I remove the offending characters so I can have sed go about the business of parsing the rest of the file for the strings in which I am interested? ... To get rid of control chars leaving only tabs and newlines, ...
    (comp.unix.shell)