我有一个 smarty 变量,我想知道它是否与某些字符串匹配
"<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 中的正则表达式进行更复杂的搜索:
{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
关于regex - Smarty 正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125918/