Friday, 23 January 2015

Options to truncate strings - PHP

If more than 40 char means did not go that link.
<?php


$long_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elith.";
$max_length = 40;

if (strlen($long_text) > $max_length)
{
$short_text = (substr($long_text,0,$max_length-1));
$short_text .= "...";
$short_text .= "<a href='http://funmin.com'>Read more</a>";
echo $short_text;
}
else
{

echo $long_text;
}

?>

0 comments:

Post a Comment