I have this code for the product description on the category page:

<?php echo substr($_product->getShortDescription(),0,40).'...'; ?>

But this code will show the ... always, even if the text length is 25 characters. How can I modify this code to show the ... only if the text is longer than 40 characters?


You can use magentos built-in method Mage_Core_Helper_String::truncate()

 * Truncate a string to a certain length if necessary, appending the $etc string.
 * $remainder will contain the string that has been replaced with $etc.
 * @param string $string
 * @param int $length
 * @param string $etc
 * @param string &$remainder
 * @param bool $breakWords
 * @return string
public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)

Like ...

echo Mage::helper('core/string')->truncate($_product->getShortDescription(), 40);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.