How to Create Read More in PHP?

If you’re developing website with PHP to list multiple articles on home page. Then definitely you will need to display only few starting lines of each article content with “Read More…” link to display full article content in a page when click on “Read More…” link. It would be best approach to display “Read More” with each article because it will allow you to display more articles in a single page that will increase readability of page. 

You may also like:

So here in this post, you will learn how to create “Read More…” link and clicking on it will display full content in a page.

Here is an example of creating read more link of content to display few line of content with “Read More…” and by clicking “Read More…” will display full content in page.

<?php
$content = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem 
accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab 
illo inventore veritatis et quasi architecto beatae vitae dicta sunt 
explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut 
odit aut fugit, sed quia consequuntur magni dolores eos qui ratione 
voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum 
quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam 
eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. 
Ut enim ad minima veniam";
// your page id to display full content
$page_id = 456;
// your page file to display full content
$link = "page.php";
// limit content character
$limit = 100;
// Called readMore() function to convert full content to read more link
echo readMore($content,$link,"id",$page_id, $limit, $limit);
?>

The below code will display content with few lines with “Read More…” link.


This is a readMore() function to create read more link of a content.

<?php
// Function to create read more link of a content with link to full page
function readMore($content,$link,$var,$id, $limit) {
$content = substr($content,0,$limit);
$content = substr($content,0,strrpos($content,' '));
$content = $content." <a href='$link?$var=$id'>Read More...</a>";
return $content;
}
?>

You may also like:

Here we have developed a complete read more script the help of above example code. You can see this script in action from below live demo link.

Demo Download


4 thoughts on “How to Create Read More in PHP?

Comments are closed.