anujgarg Posted April 15, 2009 Share Posted April 15, 2009 Hi Everyone... I am trying to add a phrase 'Read More' after truncating the text (as we see in the various forums/blogs). I try one with regex but it was not proper. Also, please suggest me any code that can truncate only the text that exceeds 20 words and show 'Read More' only after that. Please suggest me any link for the same. Any help/suggestion will be highly appreciated. TIA Anuj Link to comment https://forums.phpfreaks.com/topic/154247-read-more/ Share on other sites More sharing options...
mrMarcus Posted April 15, 2009 Share Posted April 15, 2009 try something like this : $your_words = "some text goes here. can either come from a database or just some simple text like this. am i at 20 yet? i think i just might be past now."; $words = explode (' ', $your_words); $discard = array_pop ($words); //lose the last word .. usually not a full word; $word_limit = 20; //number of words allowed; $words = implode (' ', array_slice($words, 0, $word_limit)); //reassemble paragraph; echo $words; Link to comment https://forums.phpfreaks.com/topic/154247-read-more/#findComment-810928 Share on other sites More sharing options...
mandred Posted April 15, 2009 Share Posted April 15, 2009 You could use the substr function and do it by character count too. <?php // $the_article is the writing you want cutoff with a 'Read More' link at the end $cutoff = 300; // characters to show before [read more] link $message = substr($the_article, 0, $cutoff); echo "$message...<a href=\"http://www.url.com\">Read More</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/154247-read-more/#findComment-810934 Share on other sites More sharing options...
anujgarg Posted April 16, 2009 Author Share Posted April 16, 2009 thanks for your help.... Link to comment https://forums.phpfreaks.com/topic/154247-read-more/#findComment-811406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.