note 70202 modified in function.str-word-count by danbrown

From: 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;&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$threshold_length = 80; // 80 words max&#13;&#10;$phrase = &#34;....&#34;; // populate this with the text you want to display&#13;&#10;$abody = str_word_count($phrase,2);&#13;&#10;if(count($abody) &#62;= $threshold_length){ // gotta cut&#13;&#10; $tbody = array_keys($abody);&#13;&#10; echo &#34;&#60;p&#62;&#34; . substr($phrase,0,$tbody[$threshold_length]) . &#34;... &#60;span class=\\&#34;more\\&#34;&#62;&#60;a href=\\&#34;?\\&#34;&#62;read more&#60;/a&#62;&#60;/span&#62; &#60;/p&#62;\\n&#34;;&#13;&#10;} else { // put the whole thing&#13;&#10; echo &#34;&#60;p&#62;&#34; . $phrase . &#34;&#60;/p&#62;\\n&#34;;&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;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

« previous php.notes (#190371) next »