note 75406 deleted from function.str-word-count by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:26:13 +0000 |
| Subject: | note 75406 deleted from function.str-word-count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190368@lists.php.net to get a copy of this message | ||
Note Submitter: geertdd at gmail dot com
----
Here's a very fast word limiter function that preserves the original whitespace.
<?php
function word_limiter($str, $limit = 100, $end_char = '…') {
if (trim($str) == '')
return $str;
preg_match('/\s*(?:\S*\s*){'. (int) $limit .'}/', $str, $matches);
if (strlen($matches[0]) == strlen($str))
$end_char = '';
return rtrim($matches[0]) . $end_char;
}
?>
For the thought process behind this function, please read: http://codeigniter.com/forums/viewthread/51788/
Geert De Deckere