Plugin: Simple String Match
Posted: 19 August 2009 04:46 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  28
Joined  06-04-2009

I was unable to find any plugin that could do simple string matching via the php function strpos(). Usage info below:

{exp:simple_string_match needle="word"}looking for a word{/exp:simple_string_match} outputs "1"

{exp:simple_string_match needle="word"}looking for a werd{/exp:simple_string_match} outputs "0" 

Cheers.

File Attachments
pi.simple_string_match.php.zip  (File Size: 2KB - Downloads: 188)
 Signature 

Droplab // Development

Profile
 
 
Posted: 19 August 2009 09:35 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  244
Joined  10-10-2007

Werd wink

 Signature 

.
....................................................who we are...what we do...how you benefit

Profile
 
 
Posted: 17 October 2009 09:11 PM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1753
Joined  03-26-2006

I updated this because I needed a simple conditional. I can now do this on the template:

{exp:simple_string_match needle="whatimlookinfor"}
  {if string_match}
    Yes
you found what you're looking for.
  {if:else}
    Nope, sorry.
  {/if}
{/exp:simple_string_match} 

I looked into the template class info in the docs to find out how to do simple TRUE/FALSE conditionals.

I basically removed the “eval” line and replaced it with this:

$cond = array();
$cond['string_match'= ( strpos($haystack,$needle) !== FALSE ) ? TRUE FALSE;
$haystack $FNS->prep_conditionals($haystack$cond);
$this->return_data $haystack

And don’t forget to add “$FNS” to your global vars: “global $TMPL, $DB, $FNS;”

 Signature 

ryan masuga
—————
Masuga Design | devot:ee
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 17 October 2009 09:50 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  331
Joined  02-05-2009

Ryan,

Where does the haystack go in your example?

 Signature 

TucsonSentinel.com - we’re watching Tucson

Profile
 
 
Posted: 17 October 2009 10:00 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1753
Joined  03-26-2006

Sorry, after I posted I realized my mistake. I had to re-enable the haystack. So, the important bits:

global $TMPL$DB$FNS;
          
$tagdata$TMPL->tagdata;
$haystack $TMPL->fetch_param('haystack');
$needle $TMPL->fetch_param('needle'); 

Then:

if($needle === false)
{
  
...stuff...
}
else
{
  $cond 
= array();
  
$cond['string_match'= ( strpos($haystack,$needle) !== FALSE ) ? TRUE FALSE;
  
$tagdata $FNS->prep_conditionals($tagdata$cond);
  
$this->return_data $tagdata;

Template:

{exp:simple_string_match haystack="{link_url}" needle="viewthread"}
  {if string_match}
    Hey
it's a forum link
  {if:else}
    Not a forum link
  {/if}
{/exp:simple_string_match} 

Doing it that way could get dicey if you’re passing a lot of stuff through the haystack-as-parameter (perhaps if there are quotes or weird characters) but I’m only searching a small field. I’d rather search tagdata, but I couldn’t quickly find a decent example of a way this has been done, where I could then use a simple conditional.

 Signature 

ryan masuga
—————
Masuga Design | devot:ee
@masuga | @masugadesign | @devot_ee

Profile
 
 
Posted: 17 October 2009 10:11 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  331
Joined  02-05-2009

This just might come in handy - thx to both you guys.

 Signature 

TucsonSentinel.com - we’re watching Tucson

Profile