The-Last-Escape Posted May 3, 2007 Share Posted May 3, 2007 I have been trying to find a tutorial on how to display the first X words from a database entry, like google does with search "..."'s [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]PHP: Hypertext Preprocessor PHP is a server-side HTML embedded scripting language. It provides web developers with a full suite of tools for building dynamic websites: native APIs to ... How can this be acheived? Quote Link to comment Share on other sites More sharing options...
taith Posted May 3, 2007 Share Posted May 3, 2007 <? function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...'; } echo filter_charlimit('purple monkey dishwasher',10); ?> Quote Link to comment Share on other sites More sharing options...
The-Last-Escape Posted May 3, 2007 Author Share Posted May 3, 2007 Great Stuff Quote Link to comment Share on other sites More sharing options...
taith Posted May 3, 2007 Share Posted May 3, 2007 enjoy! Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted May 3, 2007 Share Posted May 3, 2007 <?php $string = "some string goes here"; $offset = 300; //this is the min number of characters $result = substr($string, 0, strpos($string, ' ', $offset)); ?> The example by taith limits the string, the example here limits the string but makes sure the remaining word is complete and not sliced in half. Both ways are wrong though as to use this method would require you to select the complete document from the database and throw away 90+% of it in most cases, its not efficient. Quote Link to comment Share on other sites More sharing options...
The-Last-Escape Posted May 3, 2007 Author Share Posted May 3, 2007 Thanks to all Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.