Posted by: prajapatinilesh | May 16, 2012

truncate description — php read more link

These are some solutions to provide read more link using PHP
<?php

1)

// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 500) {
// truncate string
$stringCut = substr($string, 0, 500);
// make sure it ends in a word.
$string = substr($stringCut, 0, strrpos($stringCut, ‘ ‘)).’… <a href=”full link of read more page”>Read More</a>’;
}
echo $string;

2)

$num_words = 500; $words = array();
$words = explode(” “, $original_string, $num_words);
$shown_string = “”;
if(count($words) == 500){
$words[499] = ” … “;
}
$shown_string = implode(” “, $words);

3)

list($output)=explode(“\n”,wordwrap(strip_tags($str),500),1);
echo $output. ‘ … <a href=”#”>Read more</a>’;

4)

function split_words($string, $nb_caracs, $separator){
$string = strip_tags(html_entity_decode($string));
if( strlen($string) <= $nb_caracs ){
$final_string = $string;
} else {
$final_string = “”;
$words = explode(” “, $string);
foreach( $words as $value ){
if( strlen($final_string . ” ” . $value) < $nb_caracs ){
if( !empty($final_string) )
$final_string .= ” “;

$final_string .= $value;
} else {
break;
}
}//End of foreach( $words as $value )
$final_string .= $separator;
}
return $final_string;
}//End of function split_words

?>

Thanks, Nilesh.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: