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,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice