Pin Me

Understanding How to Truncate Text in PHP

written by: N Nayab•edited by: Linda Richter•updated: 6/15/2010

How to truncate text in PHP?The truncate() function suffices, but to make sense out of the truncated text, additional code to end the truncation at the end of a word or sentence helps.

  • slide 1 of 6

    The need to truncate text in PHP arises to preserve page layout when creating dynamic web pages that source content from external RSS feeds, databases, or any other external sources. If the source text is too long, the page layout goes awry. The most common solution to retain the page layout is truncating the text to fit on the page.

    Truncating the text also serves to create an excerpt, a preview, or a summary from a very long text.

  • slide 2 of 6

    The Truncate Function in PHP

    The “truncate” function is straightforward in PHP and works as follows:

    $text = With an expert writer network and an active community of life-long enthusiasts Bright Hub provides a level of technology transparency rarely seen in high tech. The team of writers and managing editors utilizes deep domain expertise to focus on creating valuable information for both novice and advanced consumers. With a content inventory of thousands of science and technology articles, software and hardware reviews, buyer’s guides, blog entries, and forum discussions, Bright Hub is able to provide readers with a dependable resource to make informative technology decisions.

    The command "truncate($text,50)" generates the first 50 characters of $text, as follows.

    With an expert writer network and an active commu

    As the above example shows, the result of the truncate() function usually cut off words and sentences at inappropriate points.

  • slide 3 of 6

    PHP Code to Truncate Text at End of Sentence

    PHP Code to Truncate Textb at End of Sentence The PHP code to truncate text but retain its meaning is as follows:

    // Original PHP code by Chirp Internet: www.chirp.com.au

    function myTruncate($text, $limit, $break=".", $pad="...")

    {

    if(strlen($text) <= $limit) return $text;

    if(false !== ($breakpoint = strpos($text, $break, $limit)))

    {

    if($breakpoint < strlen($text) - 1)

    { $text = substr($text, 0, $breakpoint) . $pad; }

    }

    return $text;

    }

    $text refers to the original text that requires truncation.

    $limit refers to the number of characters needed that needs display after the truncation.

    $break refers to a character, usually a full stop (.) that indicates where to break the text for truncation.

    $pad refers to any suffix for addition to the end of the truncated text, usually (…).

    strlen() returns the length of the string ($text in this case).

    strpos() returns the position of the first occurrence of a string inside another string ($text in this case).

    The code checks and returns the $text unchanged if the length of $text is shorter than the figure indicated in $limit.

    The code then checks if the character indicated in $break (.) is present beyond the $limit figure in $text till the end of $text.

    If the $break (.) is present beyond $limit and before the end of $text, the truncation happens by default at this point (.).

    The following code displays the first 300 characters of $text and continues till the next “.”

    $shortdesc = myTruncate($text, 300, " "); echo "<p>$shortdesc</p>";

    The following code displays the first 300 characters of $text and continues till the next white space, or the end of the word. This brings the final text length closer to $limit

    $shortdesc = myTruncate($description, 300, " "); echo "<p>$shortdesc</p>";

  • slide 4 of 6

    To truncate the text at the preceding $break character (.) instead of the (.) that follows the $limit figure, substitute the following code for the myTruncate () function

    function myTruncate($text, $limit, $break=".", $pad="...")

    {

    if(strlen($text) <= $limit) return $text;

    $text = substr($text, 0, $limit);

    if(false !== ($breakpoint = strrpos($text, $break)))

    {$text = substr($text, 0, $breakpoint); }

    return $text . $pad;

    }

  • slide 5 of 6

    PHP Code to Truncate Text at End of Word

    PHP Code to Truncate Text at End of Word In the code above, if the $break (.) is not present beyond $limit, then this command does not apply, and the text is truncated at the number of characters indicated in $limit. This creates problems in long sentences.

    The PHP code to truncate text at the end of word, by counting the number of words reads:

    ?PHP

    $numwords = 60;

    preg_match("/([\S]+\s*){0,$numwords}/", $text, $regs); $shortdesc = trim($\regs[0]);

    The above command matches up to 60 occurrences of one or more non-space characters followed by zero or more white space characters, meaning it truncates to the first ten words in the text.

  • slide 6 of 6

    References

    Chirp Internet. www.chirp.com.au

    Art-of-The-Web. PHP: Truncating Text

    w3schools. PHP String Functions