Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Truncating a string in PHP (http://www.go4expert.com/articles/truncating-string-php-t1846/)

pradeep 9Nov2006 17:58

Truncating a string in PHP
 
Many times its required that we truncate a long string and add ellipsis to the end of the truncated string. Here's a function which does exactly that.

Code: PHP

function truncate( $varb, $num )
 {
     $dnum = intval($num);
     if (strlen($varb)>$dnum)
    {
       $nvarb = substr($varb, 0, $dnum);
       $nvarb .= " ...";
     }
     else if (strlen($varb)<$dnum)
    {
       $nvarb=$varb;
     }
     return $nvarb;
 }



All times are GMT +5.5. The time now is 18:03.