Php > Strings
Short text sample from long text articles
Short text sample from long text articles When you want to show a snippet of text for an article but dont want to cut off words in the middle, use this little function. 0)) { $split_text = explode(" ", $source_text, $word_count); if (sizeof($split_text) < $word_count) { $long_enough = FALSE; } else { array_pop($split_text); $source_text = implode(" ", $split_text); } } return $long_enough; } // sample use $long_text="a b c d e f g h i j k l m n o p q r s t u v w x y z"; $short_text=$long_text; $more_text_available=shortenText($short_text, 20); echo $short_text; if($more_text_available) { echo '
more...
'; } ?> The text is shortened to the required word length. The returned value tells us if the original text is shorter than the required word length. This can then be used to determine if we want to use a link or script to display the full article.
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage