|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
pointbweb Smarty n00b
Joined: 14 May 2003 Posts: 3 Location: Seattle, WA
|
Posted: Wed May 14, 2003 4:35 am Post subject: Using wildcards for string compares |
|
|
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 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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed May 14, 2003 7:34 am Post subject: |
|
|
you can use php-functions inside if:
{if substr_count($haystack, $needle)} ... {/if} |
|
Back to top |
|
pointbweb Smarty n00b
Joined: 14 May 2003 Posts: 3 Location: Seattle, WA
|
Posted: Wed May 14, 2003 2:16 pm Post subject: |
|
|
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!
Thanks. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed May 14, 2003 2:29 pm Post subject: |
|
|
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 |
|
pointbweb Smarty n00b
Joined: 14 May 2003 Posts: 3 Location: Seattle, WA
|
Posted: Wed May 14, 2003 6:11 pm Post subject: |
|
|
Ok - I missed that. I'm new to PHP as well
Thanks a bunch. |
|
Back to top |
|
|
|
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
|