Truncating a string in PHP

pradeep's Avatar author of Truncating a string in PHP
This is an article on Truncating a string in PHP 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;
 }