rockinaway Posted May 12, 2007 Share Posted May 12, 2007 I have selected a large body of text out of my database and what to only show 200 words, then show a read more link. How can I apply this limit? Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/ Share on other sites More sharing options...
clown[NOR] Posted May 12, 2007 Share Posted May 12, 2007 use substr() <?php $text = "this is just a sample text"; $length = strlen($text); if ($length > 200) { echo substr($text, 0, 200)."... [Read more]"; } else { echo $text; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251398 Share on other sites More sharing options...
taith Posted May 12, 2007 Share Posted May 12, 2007 here you be <? function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...'; } echo filter_charlimit($row[text],200); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251414 Share on other sites More sharing options...
rockinaway Posted May 12, 2007 Author Share Posted May 12, 2007 That works, however it breaks up words.. I want 200 full words... :S Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251478 Share on other sites More sharing options...
kathas Posted May 12, 2007 Share Posted May 12, 2007 Explode your string into an array with words and select the first 200 elements of the array... Kathas Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251481 Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 you can try this. It is not optimum and a little long winded, but it works. <?php $string=' Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi ut erat. Vestibulum nisl ante, consectetuer sed, fringilla a, tincidunt ac, leo. Curabitur a ligula luctus quam pretium luctus. Ut augue dolor, hendrerit non, ultricies a, ornare eu, sapien. Duis venenatis aliquet justo. Donec dui enim, pharetra id, adipiscing vitae, euismod id, dolor. Proin viverra massa sed purus. Nunc aliquam lorem vitae tortor. Donec ornare. Aenean ultricies, augue id venenatis commodo, libero quam tincidunt justo, a tempor diam eros sed metus. Aenean luctus sem non quam. Morbi fringilla erat id risus. Cras dui. Nunc suscipit lobortis dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras pretium porta quam. Vivamus elementum ullamcorper nunc. Vestibulum orci. In blandit pellentesque odio. Maecenas gravida. Vestibulum sit amet lectus in nisi porttitor semper. Phasellus non elit ut leo aliquet pharetra. Praesent sagittis luctus nisl. Vestibulum enim. Aenean faucibus, massa nec vulputate ornare, metus diam interdum massa, id vestibulum odio turpis in nulla. Nullam vitae lectus vel augue commodo tempus. Aliquam rutrum sem at ante. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus eleifend sapien sed libero. Morbi nec nibh nec massa lobortis hendrerit. Proin blandit interdum velit. Phasellus semper. Fusce eros. Morbi a nunc. Nam at lorem at neque placerat tristique. Morbi tincidunt, purus ac cursus faucibus, est nisi pretium mauris, eget iaculis nibh lectus eget metus. Aenean magna elit, fringilla nec, pulvinar sed, dapibus vitae, eros. Praesent congue elit ac ante. In a dui id lorem imperdiet laoreet. Nam eleifend metus nec purus. Donec in nisi eget lacus aliquam condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Proin ligula ipsum, elementum quis, volutpat non, varius a, pede. Curabitur quis tellus. Quisque nisi augue, rutrum a, aliquam sed, vehicula vel, turpis. Lorem ipsum dolor. '; $split=explode(' ',$string); $x=0; while($x < 201) { echo $split[$x].' '; $x++; } echo '..... <a href="/somepage.php">Read More</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251488 Share on other sites More sharing options...
rockinaway Posted May 12, 2007 Author Share Posted May 12, 2007 PERFECT! I cleaned it up abit.. put it into a for{} .. but perfect.. now to sort out my MySQL issues! THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/51075-max-wordsthen-read-more/#findComment-251495 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.