Posted by: prajapatinilesh | July 9, 2012

display short description from full description using php

function shortDescription($fullDescription) {
$shortDescription = ”;

$fullDescription = trim(strip_tags($fullDescription));

if ($fullDescription) {
$initialCount = 125;
if (strlen($fullDescription) > $initialCount) {
//$shortDescription = substr(strip_tags($fullDescription),0,$initialCount).”…”;
$shortDescription = substr($fullDescription,0,$initialCount).”…”;
}
else {
return $fullDescription;
}
}

return $shortDescription;
}//End of function shortDescription

OR

function readMoreLink($inputParamArray) {

$description = $inputParamArray[‘description’];
$pageLink    = $inputParamArray[‘pageLink’];
$readMoreLinkClass = $inputParamArray[‘readMoreLinkClass’];

if (trim($description) == ”) {
return ”;
}

$charLimitToDisplay = 255;

$description = strip_tags($description);

if (strlen($description) > $charLimitToDisplay) {
// truncate string
$stringCut = substr($description, 0, $charLimitToDisplay);
// make sure it ends in a word so assassinate doesn’t become ass…
$description = substr($stringCut, 0, strrpos($stringCut, ‘ ‘)).’… <a href=”‘.$pageLink.'” target=”_parent”>Read More &gt;&gt;</a><br>’;
}
}

Advertisements

Responses

  1. Nilesh,

    In the following code you are checking only empty string.

    if (trim($description) == ”) {
    return ”;
    }

    I think you should be checking empty and null.


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: