Jump to content

Read More...


anujgarg

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.