How to truncate product name properly in Magento 2?

I am using 2.3.1

I have truncated product name in the listing page and displayed dots if the length of the name is more than 27 but the problem is that there are special characters like &,’,” etc. in the product name. So, I used html_entity_decode function to properly decode all special character. it is working fine for some cases

Now I have another scenario where the product name is having double quote two times in a consistent position. So, when I tried to truncate the product name the first double quote is displayed properly but the second one is not decoded properly.

Product Name: 18inch woodproduct””Iron Box

I have used the below code:

<a class="" href="<?= /* @escapeNotVerified */ $  _product->getProductUrl() ?>">    <?php       $  product_name = $  _product->getName();      //$  string = strval($  product_name);       $  len = strlen($  product_name);       if($  len > 27) {         echo html_entity_decode(substr($  product_name,0,27),ENT_QUOTES).'...';           } else {         echo $  product_name;       }     ?> </a> 

result in the front end listing page: 18inch woodproduct”&qu

How can I solve this issue?