You are here:  » Regex Search & Replace Filter with Trigger Field


Regex Search & Replace Filter with Trigger Field

Submitted by ChrisNBC on Thu, 2016-12-08 16:11 in

Hi David,

Hope all is well.

I wondered if you might be able to help me with a filter. In summary, I’m standardising merchant sizes using regex filters. It’s going well and nearly complete but I have a problem where the regex ‘rules’ need to be different for mens and womens sizes. Quite a while back you wrote a filter (“Alternative If Set DB Trigger”) which replaces text in a field if text in a ‘trigger field’ matches a regex rule. I wondered if it might be possible to create a variation of this filter so that I can set a trigger field (and trigger regex) and if it’s matched then the existing “Search and Replace RegExp_v1” is run? I tried writing a combined filter but so far no joy…

Thanks in advance.

Best regards
Chris

Submitted by support on Fri, 2016-12-09 11:55

Hello Chris,

Have a go with the following "Search and Replace RegExp Triggered"...

  /*************************************************/
  /* searchReplaceRegExpTriggered */
  /*************************************************/
  $filter_names["searchReplaceRegExpTriggered"] = "Search and Replace RegExp Triggered";
  function filter_searchReplaceRegExpTriggeredConfigure($filter_data)
  {
    print "Trigger Field:<br />";
    print "<input type='text' name='trigger_field' value='".widget_safe($filter_data["trigger_field"])."' />";
    widget_errorGet("search");
    print "Trigger Regexp:<br />";
    print "<input type='text' name='trigger_regexp' value='".widget_safe($filter_data["trigger_regexp"])."' />";
    widget_errorGet("search");
    print "Search RegExp:<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_searchReplaceRegExpTriggeredValidate($filter_data)
  {
  if (!$filter_data["search"])
  {
    widget_errorSet("search","required field");
  }
  }
  function filter_searchReplaceRegExpTriggeredExec($filter_data,$text)
  {
    global $filter_record;
    if (preg_match($filter_data["trigger_regexp"],$filter_record[$filter_data["trigger_field"]]))
    {
      return trim(preg_replace(filter_recordPlaceholders($filter_data["search"]),$filter_data["replace"],$text));
    }
    else
    {
      return $text;
    }
  }

Trigger Field is as per the parsed record (so field names corresponding with the sample data shown on Feed Registration Step 2 for the associated feed) as I see you are already referring to fields in this way in other filters in the modified includes/filters.php (e.g. Explode From Field)...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2016-12-09 14:58

Hi David,

Thanks the above mod. Ideally, I would like to apply the ‘triggered’ filters globally rather than by individual feed. Could you suggest if there is a way to do this given the filter trigger field needs to be set to the parsed field name? Is there a way I can specify the dB field name instead?

Thanks in advance.

Best regards
Chris

Submitted by support on Fri, 2016-12-09 15:09

Hi Chris,

Sure - I think you already have $importRecord declared as global but double check that in your modified includes/admin.php you have the following code around line 149:

    global $importRecord;

And then have a go with the following alternative to the above, using database field name instead of feed (e.g. any field as defined in $config_fieldSet in config.advanced.php)...

  /*************************************************/
  /* searchReplaceRegExpTriggered */
  /*************************************************/
  $filter_names["searchReplaceRegExpTriggered"] = "Search and Replace RegExp Triggered";
  function filter_searchReplaceRegExpTriggeredConfigure($filter_data)
  {
    print "Trigger Field:<br />";
    print "<input type='text' name='trigger_field' value='".widget_safe($filter_data["trigger_field"])."' />";
    widget_errorGet("search");
    print "Trigger Regexp:<br />";
    print "<input type='text' name='trigger_regexp' value='".widget_safe($filter_data["trigger_regexp"])."' />";
    widget_errorGet("search");
    print "Search RegExp:<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_searchReplaceRegExpTriggeredValidate($filter_data)
  {
  if (!$filter_data["search"])
  {
    widget_errorSet("search","required field");
  }
  }
  function filter_searchReplaceRegExpTriggeredExec($filter_data,$text)
  {
    global $importRecord;
    if (preg_match($filter_data["trigger_regexp"],$importRecord[$filter_data["trigger_field"]]))
    {
      return trim(preg_replace(filter_recordPlaceholders($filter_data["search"]),$filter_data["replace"],$text));
    }
    else
    {
      return $text;
    }
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com