Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

New Plugin: contains

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
JasonDS
Smarty Rookie


Joined: 25 Jan 2007
Posts: 25

PostPosted: Sun Feb 11, 2007 8:54 am    Post subject: New Plugin: contains Reply with quote

About: contains() finds the number of times a phrase is found in a string, and counts how many times it was found.
Usage: {$foo|contains:"bar"}
Optional Arguments: By default it isn't case-sensative, to make it case-sensative, add a second argument: {$foo|contains:"bar", true}

File: ..\libs\plugins\modifier.contains.php
Contents:
Code:

<?php
/**
 * Smarty shared plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Function: smarty_contains
 * Purpose:  Used to find a string in a string
 * Example: contains( 'Jason was here', 'here' ) returns true
 * Example2: contains( 'Jason was here', 'ason' ) returns false
 * @author Jason Strese <Jason dot Strese at gmail dot com>
 * @param string
 * @return string
 */
function smarty_modifier_contains($string, $find, $cases = false)
{
   if(!empty($string) )
   {
       if($cases)
          $string2 = str_replace( $find, null, $string );
       else
          $string2 = str_replace( strtolower($find), null, strtolower( $string ) );
       
       $count = count( str_word_count( $string, 1 )  ) - count( str_word_count( $string2, 1 ) );
       
      return $count;
   }
   
   return 0;
}

/* vim: set expandtab: */

?>


Note: If you know booleans (true/false), you know that if something numerical is returned, and it is greater than 1, it is considered true, and if its below 1, it's false. So you can use this function a couple of ways:

Code:

{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-sensative demonstration *}

{if $foo|contains:"bar":true}
      Case-sensative search returned {$foo|contains:"bar":true} results.
{/if}
Back to top
View user's profile Send private message Send e-mail
joeri210
Smarty n00b


Joined: 04 May 2008
Posts: 1

PostPosted: Sun May 04, 2008 7:22 pm    Post subject: Also for arrays Reply with quote

Or something like this.. Little bit tweaked and can also be used for arrays.

Code:

/**
 * Smarty shared plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Function: smarty_contains
 * Purpose:  Used to find a string in a string
 * 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"}
 * @author Jason Strese <Jason dot Strese at gmail dot com>
 * @param string
 * @return string
 */
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) )
   {
      if($cases)
      {
         foreach($string as $str) {
            if($str == $find) $count++;
         }
      } else {
         foreach($string as $str) {
            if(strtolower($str) == strtolower($find)) $count++;
         }
      }
   }
   return $count;
}

/* vim: set expandtab: */
Back to top
View user's profile Send private message
VTDIZ.COM
Smarty n00b


Joined: 19 Jun 2008
Posts: 2

PostPosted: Thu Jun 19, 2008 7:20 pm    Post subject: Reply with quote

Thanks for this plugin! Very helpfull!
_________________
support @ vividtemplates.net
support @ VTDIZ.COM
Back to top
View user's profile Send private message
huglester
Smarty Rookie


Joined: 09 Oct 2008
Posts: 8

PostPosted: Wed Dec 03, 2008 5:53 am    Post subject: Reply with quote

Thank you. Nice plugin.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7365
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 03, 2008 3:34 pm    Post subject: Reply with quote

Just glancing at the modifier, you might get more than you want. Example:

{$text|contains:"sock"}

If the word "socket" is in the text, you will get a match. That is probably not what you want. You could fix this with PCRE word boundaries (and at the same time, get rid of all the string replacing):

Code:
$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);


Now the word (or phrase) must be surrounded by a word boundary.


Last edited by mohrt on Mon Sep 12, 2011 9:40 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
frankc
Smarty n00b


Joined: 21 Jul 2010
Posts: 2

PostPosted: Wed Jul 21, 2010 4:39 pm    Post subject: problem with this Reply with quote

as subject reads this is not working for me ... any help ?


where id_path is "1/13"
and category.category_id = "1"


{if $category_data.id_path|contains:"$category.category_id"}
{assign var="shomesomelov" value="visible"}
{$category_data.id_path} has {$category.category_id}
{else}
{assign var="shomesomelov" value="hidden"}
{/if}




code is
<ul class="menu-subcategories {$shomesomelov}" level="{$category.level}" bassin={$category_data.id_path} hold={$category.category_id}>

this is the output:

<ul hold="13" bassin="1/13" level="1" class="menu-subcategories hidden">
Back to top
View user's profile Send private message
frankc
Smarty n00b


Joined: 21 Jul 2010
Posts: 2

PostPosted: Wed Jul 21, 2010 4:53 pm    Post subject: Reply with quote

fixed:
neeeds the preg patch


Code:
Code:
$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);
Back to top
View user's profile Send private message
ripley_one
Smarty n00b


Joined: 12 Sep 2011
Posts: 1

PostPosted: Mon Sep 12, 2011 7:02 pm    Post subject: Reply with quote

GREAT! Thanks a lot!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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