Elliott Brueggeman - PHP and Web Development Info, Photography, and More
 
Home | Web Dev Blog & Articles | PHPGraphLib | PHPSimpleChat | SkinnyTip | PHPWeatherLib | Photography | Contact
Posted on September 9, 2008 in PHP by Elliott Brueggeman4 Comments »

Here’s another PHP function I cannot live without – I add this into my standard PHP functions include on all projects. Often when displaying text, I am forced to abbreviate the text to a certain number of characters. You might jump in a decide to use substring() on your text to achieve this abbreviation, but that can cause several problems. The most blatant is that you will often split the text right in the middle of a word. In addition, if there are any HTML tags in the text, they could get cut in the middle too, or have the closing tag left off completely, potentially breaking the display or exposing the remaining part of the tag.

As a solution to these problems, I have written a function that only trims on the last space before the number of characters you specify, so it will never cut words in half. Also, it strips out HTML tags before doing the character trim, preventing possible display issues. And, as a convenience it adds ellipses (the …) to all trimmed text, as a visual cue to the reader that the text has been abbreviated.

<?php
/**
 * trims text to a space then adds ellipses if desired
 * @param string $input text to trim
 * @param int $length in characters to trim to
 * @param bool $ellipses if ellipses (...) are to be added
 * @param bool $strip_html if html tags are to be stripped
 * @return string 
 */
function trim_text($input, $length, $ellipses = true, $strip_html = true) {
	//strip tags, if desired
	if ($strip_html) {
		$input = strip_tags($input);
	}
 
	//no need to trim, already shorter than trim length
	if (strlen($input) <= $length) {
		return $input;
	}
 
	//find last space within length
	$last_space = strrpos(substr($input, 0, $length), ' ');
	$trimmed_text = substr($input, 0, $last_space);
 
	//add ellipses (...)
	if ($ellipses) {
		$trimmed_text .= '...';
	}
 
	return $trimmed_text;
}
?>

Comments

  1. Christoffer on 16 Apr 2009 at 4:04 pm

    Hi there.

    Do you have a similar function to avoid cutting tags?

    Im struggling with the html tags being cut off when i want to do a “intro” to a text.

    Say I have a large text, formatted with html through an editor. Now I want to extract the 400 first characters as a teaser on the frontpage. But if the substr accidentally cuts away a or , the whole page messes up.

    Or for instance cutting a into <img src=”/images/pictu – which also messes the entire page up.

    Catch my drift? :)

  2. Roberto on 22 Apr 2009 at 2:32 am

    with encoding ;) it counts chars better

    function trim_text($input, $length) {
    if ($strip_html) {
    $input = strip_tags($input);
    }
    if (iconv_strlen($input,’UTF-8′) <= $length) {
    return $input;
    }
    $last_space = iconv_strrpos(iconv_substr($input, 0, $length,’UTF-8′), ‘ ‘,’UTF-8′);
    $trimmed_text = iconv_substr($input, 0, $last_space,’UTF-8′);
    $trimmed_text .= ‘…’;
    return $trimmed_text;
    }

  3. Thomas Dilts on 06 May 2009 at 1:21 am

    Thanks for the function. Just what I was looking for!

  4. yvorl on 30 Jul 2009 at 1:35 am

    Hi!

    What if a word is longer than the allowed character number?

Leave a Reply - Registration Not Necessary