我有一个聪明的变量,我想知道它是否匹配某些字符串
"<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