Smarty Regular Expression Match

☆樱花仙子☆ 提交于 2019-12-10 09:37:01

问题


I Have a smarty variable, I want to know if it matches some string like

"<whatever>_thestring_<whatever>" 

where <whatever> represents any sequence of characters (or no characters).

Is there some way to test against something like *_thestring_*?


回答1:


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 />

Output:

Test haystack1 Found!
Test haystack2

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 />

Which has the output:

Test haystack1 Found!
Test haystack2 


来源:https://stackoverflow.com/questions/18125918/smarty-regular-expression-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!