Smarty正则表达式匹配 [英] Smarty Regular Expression Match
本文介绍了Smarty正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个聪明的变量,我想知道它是否与某些字符串匹配
I Have a smarty variable, I want to know if it matches some string like
"<whatever>_thestring_<whatever>"
其中<whatever>
代表任何字符序列(或没有字符).
where <whatever>
represents any sequence of characters (or no characters).
是否有某种方法可以针对*_thestring_*
之类的东西进行测试?
Is there some way to test against something like *_thestring_*
?
推荐答案
使用smarty检查另一个字符串中是否存在一个字符串:
Using smarty to check if a string exists inside another string:
{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进行更复杂的搜索:
Or you could do a more complex search using Regex in 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
这篇关于Smarty正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文