Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smarty Regular Expression Match

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_*?

like image 872
Ghazale Avatar asked Feb 15 '23 15:02

Ghazale


1 Answers

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 
like image 73
Joel Kent Avatar answered Feb 27 '23 16:02

Joel Kent