Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Using wildcards for string compares

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
pointbweb
Smarty n00b


Joined: 14 May 2003
Posts: 3
Location: Seattle, WA

PostPosted: Wed May 14, 2003 4:35 am    Post subject: Using wildcards for string compares Reply with quote

Hi - I am a smarty newbie and am trying to us an if statement that checks to see if a string contains a specific substring. Smarty doesn't like LIKE Wink and I don't think it allows wildcards either.

I tried this flawed syntax: {if $stringvariable Like "%substring%"}

Obviously flawed to you smartyer folks, but I'm sure you see what I need to do. Any help?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed May 14, 2003 7:34 am    Post subject: Reply with quote

you can use php-functions inside if:
{if substr_count($haystack, $needle)} ... {/if}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
pointbweb
Smarty n00b


Joined: 14 May 2003
Posts: 3
Location: Seattle, WA

PostPosted: Wed May 14, 2003 2:16 pm    Post subject: Reply with quote

So it would look like this: {if ($stringvar like "%substring%")} ??
Somehow this doesn't look like it would work either, but I'll try it. Remember - the problem I have is using LIKE and WILDCARDS. Your example didn't contain that, so if you could clarify, that would be great! Very Happy

Thanks.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed May 14, 2003 2:29 pm    Post subject: Reply with quote

it uses http://www.php.net/manual/en/function.substr-count.php as i said.

it counts the number of occurences of $needle inside $haystack. if the number of occurences is greater than 0, than the if-condition is true, else it's false. so it should obviously read:

{if substr_count($stringvar, "substring")} ... {/if}

to match your example.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
pointbweb
Smarty n00b


Joined: 14 May 2003
Posts: 3
Location: Seattle, WA

PostPosted: Wed May 14, 2003 6:11 pm    Post subject: Reply with quote

Ok - I missed that. I'm new to PHP as well Embarassed

Thanks a bunch.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP