douci4026 2018-12-04 14:54
浏览 45

Twig truncate()和slice()返回一个带有html实体名称的字符串

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗