Return to Snippet

Revision: 56198
at March 15, 2012 11:46 by rliverman


Initial Code
function trunCate($string, $limit, $break=".", $pad="...") {
	// return with no change if string is shorter than $limit
	if(strlen($string) <= $limit) return $string;
	// is $break present between $limit and the end of the string?
	if(false !== ($breakpoint = strpos($string, $break, $limit))){
		if($breakpoint < strlen($string) - 1){
			$string = substr($string, 0, $breakpoint) . $pad;
			}
		}
	return $string;
	}

Initial URL
http://www.the-art-of-web.com/php/truncate/

Initial Description
// quick function to cut back on the length of content

Initial Title
Truncate Text Returned by PHP

Initial Tags
php

Initial Language
PHP