How to truncate product name properly in Magento 2?How I can get string truncate method in Magento 2 like Magento 1?How do I change 'name' product attribute to storeview?Magento 2 truncate stringMagento 2: How to truncate orders tableMagento 2 : Illegal characters on importHow can I add the %-character in magento 2?Magento 2 - Product names with special charactersMagento 2: How to display brand name above product nameHow to clone/copy product in magento 2 properly wayMagento 2.3.0: product with quotation mark crashingHow to get product by name attribute

How do I subvert the tropes of a train heist?

Rotated Position of Integers

Could IPv6 make NAT / port numbers redundant?

Expenditure in Poland - Forex doesn't have Zloty

Is there an evolutionary advantage to having two heads?

Why does the UK have more political parties than the US?

What was this black-and-white film set in the Arctic or Antarctic where the monster/alien gets fried in the end?

What does it mean when you think without speaking?

Can a helicopter mask itself from Radar?

Humans meet a distant alien species. How do they standardize? - Units of Measure

If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?

Will My Circuit Work As intended?

Differences between “pas vrai ?”, “c’est ça ?”, “hein ?”, and “n’est-ce pas ?”

Why the lack of hesitance to wear pads on the sabbath?

How should I push back against my job assigning "homework"?

When a current flow in an inductor is interrupted, what limits the voltage rise?

Understanding STM32 datasheet regarding decoupling capacitors

Why were the Night's Watch required to be celibate?

Possible nonclassical ion from a bicyclic system

Is it possible to change original filename of an exe?

How to make the POV character sit on the sidelines without the reader getting bored

Can non-English-speaking characters use wordplay specific to English?

How to detach yourself from a character you're going to kill?

Using PCA vs Linear Regression



How to truncate product name properly in Magento 2?


How I can get string truncate method in Magento 2 like Magento 1?How do I change 'name' product attribute to storeview?Magento 2 truncate stringMagento 2: How to truncate orders tableMagento 2 : Illegal characters on importHow can I add the %-character in magento 2?Magento 2 - Product names with special charactersMagento 2: How to display brand name above product nameHow to clone/copy product in magento 2 properly wayMagento 2.3.0: product with quotation mark crashingHow to get product by name attribute






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















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?










share|improve this question

















  • 2





    try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

    – magefms
    May 23 at 12:00












  • @magefms still the same issue.

    – Chintan Kaneriya
    May 23 at 12:33











  • What about something like the following solution magento.stackexchange.com/a/118143/70343

    – Dominic Xigen
    May 23 at 20:37

















1















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?










share|improve this question

















  • 2





    try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

    – magefms
    May 23 at 12:00












  • @magefms still the same issue.

    – Chintan Kaneriya
    May 23 at 12:33











  • What about something like the following solution magento.stackexchange.com/a/118143/70343

    – Dominic Xigen
    May 23 at 20:37













1












1








1








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?










share|improve this question














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?







magento2 products special-character






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 23 at 11:38









Chintan KaneriyaChintan Kaneriya

378214




378214







  • 2





    try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

    – magefms
    May 23 at 12:00












  • @magefms still the same issue.

    – Chintan Kaneriya
    May 23 at 12:33











  • What about something like the following solution magento.stackexchange.com/a/118143/70343

    – Dominic Xigen
    May 23 at 20:37












  • 2





    try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

    – magefms
    May 23 at 12:00












  • @magefms still the same issue.

    – Chintan Kaneriya
    May 23 at 12:33











  • What about something like the following solution magento.stackexchange.com/a/118143/70343

    – Dominic Xigen
    May 23 at 20:37







2




2





try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

– magefms
May 23 at 12:00






try this echo html_entity_decode(substr($product_name,0,27),ENT_QUOTES, "utf-8").'...';

– magefms
May 23 at 12:00














@magefms still the same issue.

– Chintan Kaneriya
May 23 at 12:33





@magefms still the same issue.

– Chintan Kaneriya
May 23 at 12:33













What about something like the following solution magento.stackexchange.com/a/118143/70343

– Dominic Xigen
May 23 at 20:37





What about something like the following solution magento.stackexchange.com/a/118143/70343

– Dominic Xigen
May 23 at 20:37










1 Answer
1






active

oldest

votes


















0














In summary I think you can use



MagentoFrameworkFilterFilterManager $filter



$filter = $this->filter



$this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder])



Here is an example:



https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Sales/Block/Order/Item/Renderer/DefaultRenderer.php#L168



In response to your question you could create a String.php helper with the above logic and then you can use it in a template like this



String.php



public function truncate($product_name) 
return $this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder]);



Template



$helper = $this->helper('XigenDataHelperString');
echo $helper->truncate($product_name)


No you don't have to use remainder.






share|improve this answer

























  • Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

    – Chintan Kaneriya
    May 25 at 10:06












  • Updated answer in response to second question

    – Dominic Xigen
    May 25 at 21:16











  • I have implemented your solution but still no luck

    – Chintan Kaneriya
    2 days ago











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f275855%2fhow-to-truncate-product-name-properly-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In summary I think you can use



MagentoFrameworkFilterFilterManager $filter



$filter = $this->filter



$this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder])



Here is an example:



https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Sales/Block/Order/Item/Renderer/DefaultRenderer.php#L168



In response to your question you could create a String.php helper with the above logic and then you can use it in a template like this



String.php



public function truncate($product_name) 
return $this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder]);



Template



$helper = $this->helper('XigenDataHelperString');
echo $helper->truncate($product_name)


No you don't have to use remainder.






share|improve this answer

























  • Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

    – Chintan Kaneriya
    May 25 at 10:06












  • Updated answer in response to second question

    – Dominic Xigen
    May 25 at 21:16











  • I have implemented your solution but still no luck

    – Chintan Kaneriya
    2 days ago















0














In summary I think you can use



MagentoFrameworkFilterFilterManager $filter



$filter = $this->filter



$this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder])



Here is an example:



https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Sales/Block/Order/Item/Renderer/DefaultRenderer.php#L168



In response to your question you could create a String.php helper with the above logic and then you can use it in a template like this



String.php



public function truncate($product_name) 
return $this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder]);



Template



$helper = $this->helper('XigenDataHelperString');
echo $helper->truncate($product_name)


No you don't have to use remainder.






share|improve this answer

























  • Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

    – Chintan Kaneriya
    May 25 at 10:06












  • Updated answer in response to second question

    – Dominic Xigen
    May 25 at 21:16











  • I have implemented your solution but still no luck

    – Chintan Kaneriya
    2 days ago













0












0








0







In summary I think you can use



MagentoFrameworkFilterFilterManager $filter



$filter = $this->filter



$this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder])



Here is an example:



https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Sales/Block/Order/Item/Renderer/DefaultRenderer.php#L168



In response to your question you could create a String.php helper with the above logic and then you can use it in a template like this



String.php



public function truncate($product_name) 
return $this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder]);



Template



$helper = $this->helper('XigenDataHelperString');
echo $helper->truncate($product_name)


No you don't have to use remainder.






share|improve this answer















In summary I think you can use



MagentoFrameworkFilterFilterManager $filter



$filter = $this->filter



$this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder])



Here is an example:



https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Sales/Block/Order/Item/Renderer/DefaultRenderer.php#L168



In response to your question you could create a String.php helper with the above logic and then you can use it in a template like this



String.php



public function truncate($product_name) 
return $this->filter->truncate($product_name, ['length' => 27, 'etc' => '...', 'remainder' => $remainder]);



Template



$helper = $this->helper('XigenDataHelperString');
echo $helper->truncate($product_name)


No you don't have to use remainder.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 25 at 21:14

























answered May 23 at 20:41









Dominic XigenDominic Xigen

63511




63511












  • Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

    – Chintan Kaneriya
    May 25 at 10:06












  • Updated answer in response to second question

    – Dominic Xigen
    May 25 at 21:16











  • I have implemented your solution but still no luck

    – Chintan Kaneriya
    2 days ago

















  • Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

    – Chintan Kaneriya
    May 25 at 10:06












  • Updated answer in response to second question

    – Dominic Xigen
    May 25 at 21:16











  • I have implemented your solution but still no luck

    – Chintan Kaneriya
    2 days ago
















Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

– Chintan Kaneriya
May 25 at 10:06






Is it necessary to add "remainder"? and my other question is that how can I use your code in .phtml file?

– Chintan Kaneriya
May 25 at 10:06














Updated answer in response to second question

– Dominic Xigen
May 25 at 21:16





Updated answer in response to second question

– Dominic Xigen
May 25 at 21:16













I have implemented your solution but still no luck

– Chintan Kaneriya
2 days ago





I have implemented your solution but still no luck

– Chintan Kaneriya
2 days ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f275855%2fhow-to-truncate-product-name-properly-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?