twig striptags 和 html 特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28670146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:34:27  来源:igfitidea点击:

twig striptags and html special chars

htmlsymfonytwigspecial-charactersstrip

提问by Sébastien

I am using twig to render a view and I am using the striptags filter to remove html tags. However, html special chars are now rendered as text as the whole element is surrounded by "". How can I either strip special chars or render them, while still using the striptags function ?

我正在使用 twig 来呈现视图,并且我正在使用 striptags 过滤器来删除 html 标签。但是,html 特殊字符现在呈现为文本,因为整个元素都被 "" 包围。如何在仍然使用 striptags 函数的同时去除特殊字符或呈现它们?

Example :

例子 :

{{ organization.content|striptags(" >")|truncate(200, '...') }}

or

或者

{{ organization.content|striptags|truncate(200, '...') }}

Output:

输出:

"QUI SOMMES NOUS ? > NOS LOCAUXNOS LOCAUXDepuis 1995,  Ce lieu chargé d'histtheitroade et de tradition s'inscrit dans les valeurs"

回答by Elyass

If it could help someone else, here is my solution

如果它可以帮助别人,这是我的解决方案

{{ organization.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}

You can also add a trim filter to remove spaces before and after. And then, you truncate or slice your organization.content

您还可以添加修剪过滤器以删除前后的空格。然后,您截断或切片您的组织内容。

EDIT November 2017

编辑 2017 年 11 月

If you want to keep the "\n" break lines combined with a truncate, you can do

如果你想保留“\n”断行与截断相结合,你可以这样做

{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}

{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}

回答by Jon

I had a similar issue, this worked for me:

我有一个类似的问题,这对我有用:

{{ variable |convert_encoding('UTF-8', 'HTML-ENTITIES') | raw }}

回答by Sébastien

Arf, I finally found it :

Arf,我终于找到了:

I am using a custom twig filter that just applies a php function:

我正在使用仅应用 php 函数的自定义树枝过滤器:

<span>{{ organization.shortDescription ?: php('html_entity_decode',organization.content|striptags|truncate(200, '...')) }}</span>

Now it renders correctly

现在它正确呈现

My php extension:

我的 php 扩展:

<?php

namespace AppBundle\Extension;

class phpExtension extends \Twig_Extension
{

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('php', array($this, 'getPhp')),
        );
    }

    public function getPhp($function, $variable)
    {
        return $function($variable);
    }

    public function getName()
    {
        return 'php_extension';
    }
}

回答by Krzysztof Trzos

I was trying some of, among others, these answers:

我正在尝试其中的一些答案:

{{ organization.content|striptags|truncate(200, true) }}
{{ organization.content|raw|striptags|truncate(200, true) }}
{{ organization.content|striptags|raw|truncate(200, true) }}
etc.

And still got strange characters in the final form. What helped me, is putting the rawfilter on the end of all operations, i.e:

并且在最终形式中仍然有奇怪的字符。帮助我的是将raw过滤器放在所有操作的末尾,即:

{{ organization.content|striptags|truncate(200, '...')|raw }}

回答by goto

I had the same problem, I resolved it byt this function below, using strip_tags.

我遇到了同样的问题,我通过下面的这个函数使用strip_tags解决了它。

<?php

namespace AppBundle\Extension;

class filterHtmlExtension extends \Twig_Extension
{

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('stripHtmlTags', array($this, 'stripHtmlTags')),
        );
    }


    public function stripHtmlTags($value)
    {

        $value_displayed = strip_tags($value);


        return $value_displayed ;
    }

    public function getName()
    {
       return 'filter_html_extension';
    }
}