Php > Arrays
Creating teaser or preview text
Creating teaser or preview text A lot of sites will create a text "preview" on the front page of thier site (say a news story) with a "more" link to get the full story. You can easily chop a string at a certain number of characters using substr(): $charLimit = 25; $myText = "The quick brown fox jumped over the lazy dog"; $teaserText = substr($myText,0,$charLimit) . "...
more
"; echo $teaserText; ?> Which echos: "The quick brown fox jumpe... more" If you wanted to chop the text at a word break, rather than mid-word, you can do this: $wordLimit = 7; $myText = "The quick brown fox jumped over the lazy dog"; $teaserText = ''; $words = explode(' ',$myText); $i = 0; while($i < $wordLimit) { $i++; $teaserText .= $words[$i]." "; } $teaserText .= "...
more
"; ?> Which echos: "The quick brown fox jumped over the... more"
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