我有一个聪明的变量,我想知道它是否匹配一些字符串
"<whatever>_thestring_<whatever>"
其中<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 />
输出:
Test haystack1 Found!
Test haystack2
或者您可以在 smarty 中使用 Regex 进行更复杂的搜索:
{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