FUNCTION TO SHOW READ MORE LINK IN PHP.
DONT FORGET TO CLICK THANKS. ;) :p
Have you ever came across a situation where you need to truncate the large text and show the "read more.." link to redirect user to read the full post ?
I know you might be searching for a function to show read more link in php.
Here is a simple function which can be used to show the "Read more.." links dynamically.
The above function can be used anywhere to show read more links with dynamic mysql queries.
How to use this function ???
Here is a simple example to do this
First you will need to create the connection with the database.
Connection settings are stored in "connect.php" file
This will output your article text with proper linking to original post with read more link.
Even you can customize the truncate function for more security check and customizing your display.
DONT FORGET TO CLICK THANKS. ;) :p
Have you ever came across a situation where you need to truncate the large text and show the "read more.." link to redirect user to read the full post ?
I know you might be searching for a function to show read more link in php.
Here is a simple function which can be used to show the "Read more.." links dynamically.
PHP Code:
<?php
//function to truncate text and show read more link
function truncate($mytext,$link,$var,$id) {
//Number of characters to show
$chars = 25;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
$mytext = $mytext." <a href='$link?$var=$id'>read more...</a>";
return $mytext;
}
?>
The above function can be used anywhere to show read more links with dynamic mysql queries.
How to use this function ???
Here is a simple example to do this
First you will need to create the connection with the database.
Connection settings are stored in "connect.php" file
PHP Code:
<?php
include "connect.php";
$sql = "SELECT * FROM articles";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "\n";
echo truncate($row['article_text'],"article.php","article_id",$row['article_id']);
}
//function to truncate text and show read more link
function truncate($mytext,$link,$var,$id) {
//Number of characters to show
$chars = 25;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
$mytext = $mytext." <a href='$link?$var=$id'>read more...</a>";
return $mytext;
}
?>
Even you can customize the truncate function for more security check and customizing your display.
Comment