<?php
/**
 * Smarty shared plugin
 * @package Smarty
 * @subpackage plugins
 * ----------------------------------------------------------------
 * Type:     modifier
 * Name:     contains
 * Purpose:  Used to find a string in a string
 * @author Jason Strese <Jason dot Strese at gmail dot com>
 * @param string
 * @return string
 * @modified using http://www.smarty.net/forums/viewtopic.php?t=10384
 * ----------------------------------------------------------------
 * USAGE
 * Upload this file into your /plugins/ directory.
 * Example: contains( 'Jason was here', 'here' ) returns true
 * Example2: contains( 'Jason was here', 'ason' ) returns false
 * Usage string: {$foo|contains:"bar"}
 * Usage array: {$foo|@contains:"bar"}
 * Additional Examples:
	{if $foo|contains:"bar" > 5}
	It contains it more than 5 times, resulting in {$foo|contains:"bar"} matches.
	{/if}

	{* OR *}

	{if $foo|contains:"bar"}
	It contained, 'bar'.
	{/if}

	{* OR *}

	{assign var='count' value=$foo|contains:"bar"}

	{if $count}
	Contained it! ({$count} matches)
	{/if}

	{* Case-sensitive demonstration *}

	{if $foo|contains:"bar":true}
	Case-sensative search returned {$foo|contains:"bar":true} results.
	{/if}
 * ----------------------------------------------------------------
**/
function smarty_modifier_contains($string, $find, $cases = false)
{
   $count = 0;
   if( is_string($string) && !empty($string) )
   {
      if($cases) $count = substr_count($string, $find);
      else $count = substr_count(strtolower($string), strtolower($find) );
   }
   elseif( is_array($string) && count($string) )
   {   
		$find = preg_quote($find);
		if($cases) {
			$count = preg_match_all("!\b{$find}\b!",$string,$match);
		} else {
			$count = preg_match_all("!\b{$find}\b!i",$string,$match);
		}
   }
   return $count;
}//end of function
?>