Smarty regex match

I have a smarty variable, I want to know if it matches some string, e.g.

" whatever_thestring_whatever "

something like *_thestring_*

Does anyone know to do this?

+4
source share
1 answer

Using smarty to check for a string 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 can do a more sophisticated 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 an output:

 Test haystack1 Found! Test haystack2 
+6
source

Source: https://habr.com/ru/post/1495945/


All Articles