Word-based substring
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 | <?php /** * Takes a string and optionally a maximum length and 'cut' the string to match that length, based on words and not on characters. * @author Er Galvao Abbott * * @param string $pStr The string to be cutted * @param intefer $pMaxLen The maximum length to be used to cut the string * * @return string */ class Zend_View_Helper_StringCutter extends Zend_View_Helper_Abstract { public function stringCutter($pStr, $pMaxLen = 40) { if (strlen($pStr) > $pMaxLen) { $returnStr = ''; $pieces = preg_split('/b/', $pStr); foreach ($pieces as $piece) { if (strlen($returnStr . $piece) < $pMaxLen) { $returnStr .= $piece; } } $returnStr .= ' ...'; } else { $returnStr = $pStr; } return $returnStr; } } ?> |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.