1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Truncating a string in PHP

Discussion in 'PHP' started by pradeep, Nov 9, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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.

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

Share This Page