note 85563 deleted from function.str-word-count by danbrown

From: Date: Fri, 31 Aug 2012 16:25:49 +0000
Subject: note 85563 deleted from function.str-word-count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190363@lists.php.net to get a copy of this message
Note Submitter: luce ---- Fix Cathy function bug. Original Cathy function : [code] <?php function limit_text($text, $limit) { if (strlen($text) > $limit) { $words = str_word_count($text, 2); $pos = array_keys($words); $text = substr($text, 0, $pos[$limit]) . '...'; } return $text; } ?> [/code] This function return undefined index if $limit < $text. For fix it : [code] <?php function limit_text($text, $limitstr, $limitwrd) { if (strlen($text) > $limitstr) { $words = str_word_count($text, 2); if ($words > $limitwrd) { $pos = array_keys($words); $text = substr($text, 0, $pos[$limitwrd]) . '...'; } } return $text; } ?> [/code]

« previous php.notes (#190363) next »