note 85563 added to function.str-word-count
| From: | luce at osu1 dot php dot net | Date: | Sat, 06 Sep 2008 09:52:19 +0000 |
| Subject: | note 85563 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144183@lists.php.net to get a copy of this message | ||
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]
----
Server IP: 213.246.51.26
Probable Submitter: 82.121.115.12
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- https://master.php.net/note/edit/85563
Del: integrated -- https://master.php.net/note/delete/85563/integrated
Del: useless -- https://master.php.net/note/delete/85563/useless
Del: bad code -- https://master.php.net/note/delete/85563/bad+code
Del: spam -- https://master.php.net/note/delete/85563/spam
Del: non-english -- https://master.php.net/note/delete/85563/non-english
Del: in docs -- https://master.php.net/note/delete/85563/in+docs
Del: other reasons-- https://master.php.net/note/delete/85563
Reject -- https://master.php.net/note/reject/85563
Search -- https://master.php.net/manage/user-notes.php