You are here:  » if then filter


if then filter

Submitted by erv on Sun, 2012-05-13 11:51 in

hello

in some of my feeds the information i need (e.g. gender) is hidden in category data and cannot be extracted with explode or similar approaches.

e.g. the specific word is in different places for different products, or its not there at all, but some particular words might tell you that this is a product for a particular gender.

so i'm looking for something like the drop records filter with regular expressions, but instead of dropping it should replace whatever information there is with something predefined, eg. if fieldX contains (word1|word2|word3) then fill the field with "women".

maybe there is a way of doing this with existing functionality and i havent found it yet.

Submitted by support on Mon, 2012-05-14 08:38

Hi, and welcome to the forum!

Have a go with the following new filter which i've based on the preg version of Search and Replace from this thread - simply add the code to the end of includes/filter.php, just before the closing PHP tag:

  /*************************************************/
  /* pregReplaceAllIf */
  /*************************************************/
  $filter_names["pregReplaceAllIf"] = "preg Replace All If";
  function filter_pregReplaceAllIfConfigure($filter_data)
  {
    print "preg Expression:<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_pregReplaceAllIfValidate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
  }
  function filter_pregReplaceAllIfExec($filter_data,$text)
  {
    if (preg_match($filter_data["search"],$text))
    {
      return $filter_data["replace"];
    }
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by erv on Tue, 2012-05-15 07:31

hello David,

thanks, after adding a ")" to "if (preg_match($filter_data["search"],$text)" it works perfect.

at first i was surprised that it does either replace the content or delete it, but in some applications (like "gender") thats what I want anyway. however sometimes I might want to keep the content, so i took the liberty to create 2 variations of your filter.

  /*************************************************/
  /* pregReplaceAllIf or leave empty */
  /*************************************************/
  $filter_names["pregReplaceAllIf"] = "preg Replace All If else leave empty";
  function filter_pregReplaceAllIfConfigure($filter_data)
  {
    print "preg Expression:<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_pregReplaceAllIfValidate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
  }
  function filter_pregReplaceAllIfExec($filter_data,$text)
  {
    if (preg_match($filter_data["search"],$text))
    {
      return $filter_data["replace"];
    }
  }
  /*************************************************/
  /* pregReplaceAllIf or leave as is */
  /*************************************************/
  $filter_names["pregReplaceAllIf2"] = "preg Replace All If else leave as is";
  function filter_pregReplaceAllIf2Configure($filter_data)
  {
    print "preg Expression:<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_pregReplaceAllIf2Validate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
  }
  function filter_pregReplaceAllIf2Exec($filter_data,$text)
  {
    if (preg_match($filter_data["search"],$text))
    {
      return $filter_data["replace"];
    }
    else
    {
      return $text;
    }
  }

Submitted by support on Tue, 2012-05-15 08:58

Thanks erv,

Syntax corrected in my first reply also...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2014-10-20 11:07

Hi David,

Hope you had a good weekend. I wonder if you you may be able to help... I need to scan and set whole words so added the filter "scanSet (whole word match)" above but when I go to set the filter in admin, I just get a blank screen. The install is version 13/03A, I wondered if you could suggest how to resolve this?

Additionally, I know there is a filter to scan and set a particular field, please could you tell me if there is a filter to scan and set a field but for a whole word?

Thanks in advance.

Regards
Chris

Submitted by support on Mon, 2014-10-20 11:32

Hi Chris,

Attached should work as whole word Scan and Set, based on scanning the field that you want to actually set the value of rather than name + description...

  /*************************************************/
  /* scanSet (whole word match) */
  /*************************************************/
  $filter_names["scanSet"] = "Scan and Set";
  function filter_scanSetConfigure($filter_data)
  {
    print "Values:<br />";
    print "<input type='text' name='values' value='".widget_safe($filter_data["values"])."' />";
    widget_errorGet("values");
  }
  function filter_scanSetValidate($filter_data)
  {
    if (!$filter_data["values"])
    {
      widget_errorSet("values","required field");
    }
  }
  function filter_scanSetExec($filter_data,$text)
  {
    $values = explode(",",$filter_data["values"]);
    foreach($values as $value)
    {
      if (preg_match("/\b".preg_quote($text)."\b/i",$nameDesc)) return $value;
    }
  }

I've added preg_quote as the blank screen could be caused by the resulting regular expression being invalid, and also changed \s to \b (word boundary) in the expression which will match beginning / end of lines as well as spaces...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2014-10-20 15:24

Hi David,

Thanks as always for your quick response. I realised the filter name "scanSet" was already used in another filter but once I renamed all the instances in the code, I was able to set the search words However, the filter failed to have any effect.

{code saved}

Am I correct in thinking that the words to search are comma seperated?

so for example:

word1,word2,word3

Also, please could you confirm if there are any filters that work in the same way as the 'original' scan and set filter(searching both the product name and description) but only for a whole word?

Thanks in advance.

Regards
Chris

Submitted by support on Mon, 2014-10-20 15:50

Hi Chris,

Please could you email me your modified includes/filter.php and I'll check it out on my test server for you, and then also add a version to work on the name + description field as before...

Cheers,
David.
--
PriceTapestry.com