1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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