Php Twig如何在JSON中插值
我目前正在尝试从字符串中删除Unicode字符Php Twig如何在JSON中插值,php,wordpress,Php,Wordpress,我目前正在尝试从字符串中删除Unicode字符 {% include 'components/accordion.twig' with { this: { id: program.slug, active: (loop.index == 1) ? true : false } } only %} {% endfor %} 基本上我想要达到的是 {% include 'components/accordion.twig'
{% include 'components/accordion.twig' with {
this: {
id: program.slug,
active: (loop.index == 1) ? true : false
}
} only %}
{% endfor %}
基本上我想要达到的是
{% include 'components/accordion.twig' with {
this: {
id: "{{ programs.slug | convert_encoding('UTF-8', 'ISO-8859-1') }}"
active: (loop.index == 1) ? true : false
}
} only %}
{% endfor %}
问题是程序.slug
返回“aria-c-senior-citizens-rent-increase-exception-%e2%80%8bscrie”
我试图删除%e2%80%8b
,因此我通过转换_编码('UTF-8','ISO-8859-1')得到了“aria-c-senior-cizens-rent-increase-exception-scrie”
)
我该怎么做呢?只要:
{% include 'components/accordion.twig' with {
this: {
id: program.slug | convert_encoding('UTF-8', 'ISO-8859-1'),
active: (loop.index == 1) ? true : false
}
} only %}
因为
{{}
相当于echo
在木材中创建函数
我必须对程序进行解码。slug
因此我创建了一个函数,并使用
“删除unicode字符”?你知道JSON必须是utf8编码的,对吧?@Sherif我更新了我的帖子
%e2%80%8b
是URL编码的,这意味着当解码时它代表U+E2808B
或者真正的U+200B
,这是一个零宽度的空格字符。通过破坏编码来剥离它是非常迂回的。同样,%e2%80%8b
显然是URL编码的。您可能从$\u GET
或其他url编码机制中获得该编码值。通过urldecode
将其编码回有效的utf8。var_dump(urldecode('aria-c-senior-citizens-rent-increment-exception-%e2%80%8bscrie')
为您提供string(56)#aria-c-senior-cizens-rent-increment-exception-exception-scrie“
注意它是56,而不是53。那是因为那里有一个零宽度的空间。如果您想删除它:var\u dump(str\u replace(“\u{200b})”,urldecode(“#aria-c-senior-citizens-rent-increase-exception-%e2%80%8bscrie”)
,这将为您提供string(53)\aria-c-senior-cizens-rent-increase-exception-scrie
/**
* Adds functionality to Twig.
*
* @param \Twig\Environment $twig The Twig environment.
* @return \Twig\Environment
*/
function add_to_twig( $twig ) {
// Adding functions as filters.
$twig->addFilter( new Timber\Twig_Filter( 'url_decode', 'url_decode' ) );
return $twig;
}
function url_decode( $text ) {
return urldecode($text);
}
id: function('url_decode', program.slug),