note 62022 deleted from function.substr by bobby

From: Date: Thu, 14 Sep 2006 18:29:01 +0000
Subject: note 62022 deleted from function.substr by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117189@lists.php.net to get a copy of this message
Note Submitter: x11 at arturaz dot afraid dot org ---- /** * Cut string to n symbols and add delim but do not break words. * * Example: * <code> * $string = 'this is some content to be shortened'; * echo myfragment($string, 15); * </code> * * Output: 'this is some content...' * * @access public * @param string string we are operating with * @param integer character count to cut to * @param string|NULL delimiter. Default: '...' * @return string processed string **/ function myfragment($str, $n, $delim='...') { // {{{ $len = strlen($str); if ($len > $n) { preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); return rtrim($matches[1]) . $delim; } else { return $str; } } // }}}

« previous php.notes (#117189) next »