1

I have a string with 69 characters:

This is a test with characters like € or "like this". How 'bout that?

I use CKEditor in my form and make the word 'test' bold. This is how it is saved it the database:

<p>This is a <strong>test </strong>with characters like &euro; or &quot;like this&quot;. How &#39;bout that?</p>

If I want to show the string without any markup I use the Twig-filters striptags and raw after each other which results in:

This is a test with characters like € or "like this". How 'bout that?

Now of the original string I want to show the first 65 characters without any styling. I use the Twig-filter truncate (65). This is my code in Twig:

{{ string | striptags | raw | truncate (65) }}

My result is

This is a test with characters like &euro; or &quot;like this&quo...

As you can see the special characters are shown as their HTML entity name, even though I only added the truncate filter. Is this a bug or should this be expected? The same thing happens when I use the slice filter.

My result should be:

This is a test with characters like € or "like this". How 'bout 

EDIT: I understand what the issue is now. When using the raw filter on a string this does not mean any of the original characters from that string (i.e. the HTML entity names) will not be counted when using slice or truncate filter. I still have not found a solution though.

wp78de
  • 18,207
  • 7
  • 43
  • 71
Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58
  • @jeroen, they are there. They are saved in the database. I display the string in a Twig template, adding `raw` not to show the html entities. – Dirk J. Faber Dec 04 '18 at 15:15
  • And if you switch the order of `raw` and `truncate`? I think you need `raw` as the last filter. – jeroen Dec 04 '18 at 15:17
  • @jeroen, my result will be a much shorter string then and also if the cut is made on a html entity the partial entity will show. `This is a test with characters like € or "like this&quo...` is the result. – Dirk J. Faber Dec 04 '18 at 15:19
  • 1
    The `truncate`-filter is not marked as safe, as seen in the [definition](https://github.com/twigphp/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/Text.php). That means u'd need to apply `raw` twice e.g. `{{ string | striptags | raw | truncate (65) | raw }}` – DarkBee Dec 04 '18 at 15:22
  • @DarkBee, I tried that as well, results in `This is a test with characters like € or "like this&quo...`. Too short and with the cut html entity name. – Dirk J. Faber Dec 04 '18 at 15:26
  • [`striptags`](https://twig.symfony.com/doc/2.x/filters/striptags.html) is mostly a wrapper around [`strip_tags()`](http://php.net/strip_tags), which couldn't care less about HTML entities. – Álvaro González Dec 04 '18 at 15:39
  • 1
    I'm guessing you would first need to `html decode` those entities yeah – DarkBee Dec 04 '18 at 15:42

1 Answers1

0

Create a twig function, which will converts the html entities to UTF-8 ( function has been defined in this question).

class AppExtension extends AbstractExtension
{
    public function getFunctions()
    {
        return [
            new TwigFunction('unescape', [$this, 'unescape']),
        ];
    }
    public function unescape($value)
    {
        return html_entity_decode($value);
    }
}

you can then use your function in your twig template:

{{ unescape(last_article.text  | striptags)  | slice(0,150) | raw  }}

the unescape convert your htmlentities to UTF-8, the slice will be performed afterwards