› String Truncate 
Wed, 07/19/2006 - 14:26
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 = '…') { return $str; } $str = $matches[0]; # remove trailing opener tags and close all other open tags: } else { $str = $arr[0]; } return $str; }
Comments
Post new comment