Magento 2 Product Description – How to Truncate Short Description

magento2-migration-toolmagento2.2product-attribute

I've migrated Magento 1 to Magento 2, In the Magento 1 site on product listing page I've truncated the length of the short description.

e.g- echo Mage::helper('core/string')->truncate($short_description, '120');

I require the same thing in Magento 2 way, how can I achieve it?

Note:- I don't want to use simple core PHP to restrict length.

Best Answer

You can truncate string in Magento-2 by below code

Inject below parameters to your class __construct

protected $filter;

public function __construct(
        \Magento\Framework\Filter\FilterManager $filter
    ) {
        $this->filter = $filter;
   }

You can truncate string by below code

$this->filter->truncate($short_description, ['length' => 120]));
Related Topic