php 树枝中的 str_replace

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4943880/
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-25 16:19:02  来源:igfitidea点击:

str_replace in Twig

phpfilterstr-replacetwig

提问by umpirsky

I want to do a simple str_replace in my twig template. I'm new to twig and probably I need to add new filter or sth like that or to use existing. How can I do this? Where can I find list of filters available?

我想在我的树枝模板中做一个简单的 str_replace 。我是新来的树枝,可能我需要添加新的过滤器或类似的东西或使用现有的。我怎样才能做到这一点?在哪里可以找到可用的过滤器列表?

采纳答案by umpirsky

There is a replace filter see documentation

有一个替换过滤器,请参阅文档

回答by M Khalid Junaid

To replace a string which is stored in twig variables:

要替换存储在树枝变量中的字符串:

{% set twig_content_variable= 'Testing to replace content'%}
{% set replace_value_var= 'Testing' %}
{% set replace_with_value_var = 'Testing complete' %} 

{{ twig_content_variable|replace({ (replace_value_var): replace_with_value_var }) }}

回答by websky

Use this to replace |with -and replace ,width .:

使用此更换|-和替换,宽度.

{{age|replace({'|': "-", "," : "."})}}

Example input 31|60,comes out as 31-60.

示例输入31|60,结果为31-60.

回答by Denis Solakovic

Also, this could be useful for getting rid of (utf-8) language chars...

此外,这对于摆脱(utf-8)语言字符可能很有用......

{{some_string|replace({'?':'c','?':'c','?':'s','?':'z','?':'d','?':'C','?':'C','?':'S','?':'Z','?':'D'})}}

回答by Ash

When we add a single quote inside a single quote then we face the error.

当我们在单引号中添加单引号时,就会遇到错误。

solution for that:-

解决方案:-

{{description|replace({"\'": "&#039"})|raw }}