简体   繁体   English

有没有办法在 PHP Twig 中比较字符串而忽略大小写?

[英]Is there a way to compare strings ignoring case in PHP Twig?

I am working with a twig in PHP, and I need to add a way to filter the results.我正在用 PHP 处理一根树枝,我需要添加一种过滤结果的方法。 I decided to do this in the templating language by also injecting the filtering keyword.我决定在模板语言中通过注入过滤关键字来做到这一点。 (My data fetches all categories which each contain listings, and I want to filter the listings). (我的数据获取每个包含列表的所有类别,我想过滤列表)。 According to the twig documentation, there is not such a way, unless I have missed some part of the docs.根据树枝文档,没有这样的方法,除非我错过了文档的某些部分。

If I use something such as:如果我使用诸如:

{% if listing.name == filter %}
    ...
{%  endif %}

My code will not match correctly if the name is "test" and the filter is "Test".如果名称为“test”且过滤器为“Test”,我的代码将无法正确匹配。 I hope to be able to gain this functionality.我希望能够获得这个功能。

Thank you for the help!感谢您的帮助!

To expand on u_mulder's comment, the standard method is to convert both strings to lowercase (so as to avoid any unknown capitalisation in either string) and then run a comparison that checks the type as well as content.为了扩展 u_mulder 的评论,标准方法是将两个字符串都转换为小写(以避免任一字符串中出现任何未知的大写),然后运行检查类型和内容的比较。

In PHP, this looks a little like this:在 PHP 中,这看起来有点像这样:

<?php

$stringOne = 'AbcdEfg';
$stringTwo = 'Abcdeefg';

if (strtolower($stringOne) === strtolower($stringTwo)) {
    // comparison is true
} else {
    // comparison is false
}

And again, Yash Karanke is correct, read the How To Ask post to better format questions再说一次,Yash Karanke 是正确的,请阅读 How To Ask 帖子以更好地格式化问题

Thanks谢谢

D D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM