Function to show read more link in php

Started on the old forum in September 2012 · view the original in the archive · old links redirect here

5 replies · 8,527 views · started 14 years ago · 1 min read

5 participants
tkreturns Developer · 98 posts #1

***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.

<?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  
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;  
}  
?>  

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.

ASK FOR HELP

Budove Newbie · 6 posts #4

Thanks for that tkreturns!

I just recently had a need for something like that and ended up using JavaScript instead.

Added after 3 minutes:

nimeshka wrote:

include ("connect.php");  

No parenthesis are needed.

tkreturns Developer · 98 posts #5

Budove wrote: Thanks for that tkreturns!

I just recently had a need for something like that and ended up using JavaScript instead.

Added after 3 minutes:

No parenthesis are needed.

Cheers mate. Here to help. Drop a message if you need any help 🙂

ASK FOR HELP

CreativityKills Lead · 833 posts #6

thanks for this it was lovely 🙂

/**
 * Limit the number of characters in a string.
 *
 * <code>
 *		// Returns "Tay..."
 *		echo strlimit('Taylor', 3);
 *
 *		// Limit the number of characters and append a custom ending
 *		echo strlimit('Taylor', 3, '---');
 * </code>
 *
 * @param  string  $value
 * @param  int     $limit
 * @param  string  $end
 * @return string
 */
function strlimit($value, $limit = 100, $end = '...')
{
	return (strlen($value) <= $limit) ? $value : substr($value, 0, $limit).$end;
}

/**
 * Limit the number of chracters in a string including custom ending
 * 
 * <code>
 *		// Returns "Taylor..."
 *		echo strlimit_exact('Taylor Otwell', 9);
 *
 *		// Limit the number of characters and append a custom ending
 *		echo strlimit_exact('Taylor Otwell', 9, '---');
 * </code>
 * 
 * @param  string  $value
 * @param  int     $limit
 * @param  string  $end
 * @return string
 */
function strlimit_exact($value, $limit = 100, $end = '...')
{
	$limit -= strlen($end);

	return (strlen($value) <= $limit) ? $value : strlimit($value, $limit, $end);
}

/**
 * Limit the number of words in a string.
 *
 * <code>
 *		// Returns "This is a..."
 *		echo strlimit_words('This is a sentence.', 3);
 *
 *		// Limit the number of words and append a custom ending
 *		echo strlimit_words('This is a sentence.', 3, '---');
 * </code>
 *
 * @param  string  $value
 * @param  int     $words
 * @param  string  $end
 * @return string
 */
function strlimit_words($value, $words = 100, $end = '...')
{
	if (trim($value) == '') return '';

	preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);

	if (strlen($value) == strlen($matches[0]))
	{
		$end = '';
	}

	return rtrim($matches[0]).$end;
}

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close