Ticket #15 (closed Feature Request: fixed)

Opened 17 months ago

Last modified 17 months ago

Optimize text_helper.php word_limiter()

Reported by: Tido Owned by:
Priority: minor Milestone: 1.0
Component: Version:
Keywords: Cc:

Description

Optimizations by sophistry and Geert: http://codeigniter.com/forums/viewthread/51788/

Change History

Changed 17 months ago by Tido

  • type changed from bug to enhancement

Changed 17 months ago by Tido

  • priority changed from major to minor

Changed 17 months ago by Tido

  • status changed from new to closed
  • resolution set to fixed

Switched it out with Geert's latest work of beauty:

function word_limiter($str, $limit = 100, $end_char = '…') {
    
    // Don't bother about empty strings.
    // Get rid of them here because the regex below would match them too.
    if (trim($str) == '')
        return $str;
    
    // Added the initial \s* in order to make the regex work in case $str starts with whitespace.
    // Without it a string like " test" would be counted for two words instead of one.
    preg_match('/\s*(?:\S+\s*){1,'. (int) $limit .'}/', $str, $matches);
    
    // Only add end character if the string got chopped off.
    if (strlen($matches[0]) == strlen($str))
        $end_char = '';
    
    // Chop off trailing whitespace and add the end character.
    return rtrim($matches[0]) . $end_char;
}
Note: See TracTickets for help on using tickets.