String Truncate (July 19, 2006)
If you write a CMS you will have to truncate your contents to automagically create summaries. The following function will do the job:
If you write a given string (default '<!--MORE-->'
) inside one of your contents, the text will be truncated to that position.
Else the function looks for the nearest word boundary after at least $len
characters and cuts there. Because that might be directly inside your text $append
will be appended. To prevent that the markup is messed up, closetags()
is called.
/**
* if $splitter is found inside the $str, everything before $splitter will be
* returned.
* else truncates $str after $length chars (actually at the nearest
* word boundary after at least $len characters). Also $append will be added to
* $str
*
* @param string &$str
* @param integer $length
* @param optional string $hardbreak
* @param optional string $append
* @return string
* @author Milian Wolff <mail@milianw.de>
*/
function truncate($str, $len = 200, $splitter = '<!--MORE-->', $append = '…') {
if (strlen($str) <= $len) {
return $str;
}
if ($len > 0 && !strstr($str, $splitter)) {
preg_match('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$len.',}\b#U', $str, $matches);
$str = $matches[0];
# remove trailing opener tags and close all other open tags:
$str = closetags(preg_replace('#\s*<[^>]+>?\s*$#', '', $str).$append);
} else {
$arr = explode($splitter, $str, 2);
$str = $arr[0];
}
return $str;
}