note 70202 modified in function.str-word-count by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:27:24 +0000 |
| Subject: | note 70202 modified in function.str-word-count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190371@lists.php.net to get a copy of this message | ||
I found a more reliable way to print, say the first 100 words and then print elipses. My code goes
this way; <?php $threshold_length = 80;
// 80 words max $phrase = "...."; // populate this with the text
you want to display $abody =
str_word_count($phrase,2); if(count($abody) >= $threshold_length){ // gotta
cut $tbody = array_keys($abody); echo
"<p>" . substr($phrase,0,$tbody[$threshold_length]) . "...
<span class=\\"more\\"><a
href=\\"?\\">read more</a></span>
</p>\\n"; } else { // put the whole thing
echo "<p>" . $phrase .
"</p>\\n"; } ?> For
any questions, com.iname@artaxerxes2
--was--
I found a more reliable way to print, say the first 100 words and then print elipses. My code goes
this way;
$threshold_length = 80; // 80 words max
$phrase = "...."; // populate this with the text you want to display
$abody = str_word_count($phrase,2);
if(count($abody) >= $threshold_length){ // gotta cut
$tbody = array_keys($abody);
echo "<p>" . substr($phrase,0,$tbody[$threshold_length]) . "... <span
class=\"more\"><a href=\"?\">read more</a></span>
</p>\n";
} else { // put the whole thing
echo "<p>" . $phrase . "</p>\n";
}
For any questions, com.iname@artaxerxes2
http://php.net/manual/en/function.str-word-count.php