我有一个 smarty 变量,我想知道它是否与某些字符串匹配
"<whatever>_thestring_<whatever>"
where <whatever>
表示任何字符序列(或无字符)。
有没有什么方法可以测试类似的东西*_thestring_*
?
使用 smarty 检查一个字符串是否存在于另一个字符串中:
{assign "haystack1" "whatever_thestring_whatever"}
{assign "haystack2" "whatever_thestrings_whatever"}
Test haystack1 {if $haystack1|strstr:"_thestring_"}Found!{/if}<br />
Test haystack2 {if $haystack2|strstr:"_thestring_"}Found!{/if}<br /><br />
Output:
Test haystack1 Found!
Test haystack2
或者您可以使用 smarty 中的正则表达式进行更复杂的搜索:
{assign "haystack1" "whatever_thestring_whatever"}
{assign "haystack2" "whatever_thestrings_whatever"}
{assign "check_haystack1" $haystack1|regex_replace:"/_thestring_/":" "}
{assign "check_haystack2" $haystack2|regex_replace:"/_thestring_/":" "}
Test haystack1 {if $check_haystack1 !== $haystack1}Found!{/if}<br />
Test haystack2 {if $check_haystack2 !== $haystack2}Found!{/if}<br />
其输出为:
Test haystack1 Found!
Test haystack2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)