|

|
Trimming Descriptions and Reviews
Often you would like to trim a product description or customer review to a shorter length. The following PHP function will trim a string to a desired length. It breaks the string at a space.
function cutString($text, $limit)
{
$text = strip_tags($text);
if (strlen($text) > $limit)
{
$x = $limit;
while ($x > ($limit * .8))
{
--$x;
if (substr($text, $x, 1) == ' ') break;
}
$text = substr($text, 0, $x);
return $text . "...";
}
else
{
return $text;
}
}
|
|
|
|