You are here:  » Delete from field filter if matched with another field


Delete from field filter if matched with another field

Submitted by ChrisNBC on Thu, 2014-10-23 09:34 in

Hi David,

Hope all is going well. I wondered if you could tell me if there is a filter which would allow me to search the contents of a field to see if it contained the contents of another field and if it did then the matched words contained in the searched field would be deleted?

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-10-23 10:03

Hello Chris,

Have a go with the following;

  /*************************************************/
  /* Drop Words */
  /*************************************************/
  $filter_names["dropWords"] = "Drop Words";
  function filter_dropWordsConfigure($filter_data)
  {
    print "Words:<br />";
    print "<input type='text' name='words' value='".widget_safe($filter_data["words"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Field:<br />";
    print "<input type='text' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
  }
  function filter_dropWordsValidate($filter_data)
  {
    if (!$filter_data["words"])
    {
      widget_errorSet("words","required field");
    }
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
  }
  function filter_dropWordsExec($filter_data,$text)
  {
    global $filter_record;
    $words = explode(",",$filter_data["words"]);
    foreach($words as $word)
    {
      $word = trim($word);
      if (preg_match("/\b".preg_quote($word)."\b/i",$filter_record[$filter_data["field"]]))
      {
        $text = str_replace($word,"",$text);
      }
    }
    $text = preg_replace('/[ ]{2,}/',' ',$text);
    return $text;
  }

Words can be entered as a comma separated list, and Field entered exactly as the field names appear in the sample data as shown on Feed Registration Step 2. Each word will then be checked for as a whole-word match (using preg_match with the \b "word boundary" operators) in the field specified and removed from the field to which the filter is being applied if matched. Finally, any double (or more) spaces that result from the replacements are replaced with a single space.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2014-10-23 10:12

Hi David,

Thanks for your quick response. Could you suggest if there is a way of using the filter above so that instead of using a comma separated list the check is done against the contents of another field? This will negate the risk of the merchant using slight variations in the wording as they split some of the content of the field to be matched into separate fields. I remember a function using %field%, I'm wondering if it would be possible to use this?

Thanks in advance.

Regards
Chris

Submitted by ChrisNBC on Thu, 2014-10-23 10:54

Hi David,

Thanks for the attached. Please ignore my previous post, I missed that there was an option to use a comma separated list OR a field. There seems to be a problem with it though as when I enter the field and press enter nothing happens? Would be grateful if you could suggest how this might be resolved.

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-10-23 11:26

Hi Chris,

Multiple words (comma separated) is working OK on my test server - do you meant that you're not able to Save the filter, and then return to it from the filters page and see your configuration preserved?

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2014-10-23 11:57

Hi David,

Sorry, now I'm confused!...

It might be easier if I explain what I'm trying to do...

In my Product Name field I have a product called "BrandXYZ Type Colour"
In the brand field I then have just the brand so for this example 'BrandXYZ'

What I want to do is check the product name field for the word in the brand field (BrandXYZ) and if it's matched delete the full word from the product name field...

I'm a little confused now if the filter provided will do this as there are two field boxes in the filter set up...

Would be grateful if you could clarify.

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-10-23 12:26

Hi Chris,

Ah, I understand - have a go with this version:

  /*************************************************/
  /* Drop Words */
  /*************************************************/
  $filter_names["dropWords"] = "Drop Words";
  function filter_dropWordsConfigure($filter_data)
  {
    print "Field:<br />";
    print "<input type='text' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
  }
  function filter_dropWordsValidate($filter_data)
  {
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
  }
  function filter_dropWordsExec($filter_data,$text)
  {
    global $filter_record;
    $word = $filter_record[$filter_data["field"]];
    if (preg_match("/\b".preg_quote($word)."\b/i",$text))
    {
      $text = str_replace($word,"",$text);
    }
    $text = preg_replace('/[ ]{2,}/',' ',$text);
    return $text;
  }

Apply the filter to the field you want to change (e.g. Product Name), and then in the single configuration parameter "Field", enter the exact field name, as before as shown on Feed Registration Step 2, of the field you want to scan for in the Product Name and remove if found...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-10-24 14:25

Hi David,

I'm tring to use the first filter you provided above to delete words in a field by specifying a comma separated list of words to search for. I modified the field so it does not clash with the second version which you provided but I must have got something wrong somewhere as it does not have any impact on the data when it's run. I wondered if you could suggest what could be preventing it from working?

The version I have is:

  /*************************************************/
  /* Drop Words List */
  /*************************************************/
  $filter_names["dropWordslist"] = "Drop Words List";
  function filter_dropWordslistConfigure($filter_data)
  {
    print "Words:<br />";
    print "<input type='text' name='words' value='".widget_safe($filter_data["words"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Field:<br />";
    print "<input type='text' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
  }
  function filter_dropWordslistValidate($filter_data)
  {
    if (!$filter_data["words"])
    {
      widget_errorSet("words","required field");
    }
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
  }
  function filter_dropWordslistExec($filter_data,$text)
  {
    global $filter_record;
    $words = explode(",",$filter_data["words"]);
    foreach($words as $word)
    {
      $word = trim($word);
      if (preg_match("/\b".preg_quote($word)."\b/i",$filter_record[$filter_data["field"]]))
      {
        $text = str_replace($word,"",$text);
      }
    }
    $text = preg_replace('/[ ]{2,}/',' ',$text);
    return $text;
  }

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2014-10-24 15:42

Hi Chris,

Just to make sure I understand the use case, please could you make an example just as you did above e.g. field value before, how to configure filter, and the expected result after?

Thanks!
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-10-24 16:07

Hi David,

Sure, to give an example, The field value could be:

"BrandXYZ TypeXYZ Blue"

What I would like to do is search for a word/s within the field and delete any matches, so using the example above I would like to delete the words 'blue,yellow,green' if they are present in the field so the result will be:

"BrandXYZ TypeXYZ"

Hope this helps?

Regards
Chris

Submitted by support on Fri, 2014-10-24 16:17

Hi Chris,

No problem - no need for the field parameter etc. so the below will do the trick;

  /*************************************************/
  /* Drop Words List */
  /*************************************************/
  $filter_names["dropWordslist"] = "Drop Words List";
  function filter_dropWordslistConfigure($filter_data)
  {
    print "Words:<br />";
    print "<input type='text' name='words' value='".widget_safe($filter_data["words"])."' />";
    widget_errorGet("search");
  }
  function filter_dropWordslistValidate($filter_data)
  {
    if (!$filter_data["words"])
    {
      widget_errorSet("words","required field");
    }
  }
  function filter_dropWordslistExec($filter_data,$text)
  {
    $words = explode(",",$filter_data["words"]);
    foreach($words as $word)
    {
      $word = trim($word);
      if (preg_match("/\b".preg_quote($word)."\b/i",$text))
      {
        $text = str_replace($word,"",$text);
      }
    }
    $text = preg_replace('/[ ]{2,}/',' ',$text);
    return $text;
  }

Attach the filter to the field concerned (e.g. Product Name), and enter comma separated list of words to strip as required...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2014-11-03 13:12

Hi David,

Hope you had a good weekend. Apologies for the delay in responding.

Thanks for the new filter above which works perfectly.

Best regards
Chris

Submitted by ChrisNBC on Fri, 2014-11-07 17:07

Hi David,

I wondered if you might be able to tell me if there are any existing filters which would allow me to use regular expression to delete matches from a field?

I searched the forum but couldn't spot anything.

I'm hoping that maybe there is something already that would work in the same way as the "Drop Words List" but instead of specifying a word list I would like to input a regular expression?

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2014-11-07 18:38

Hi Chris,

Have a go with Search and Replace RegExp - that should do the trick - regular expression as the Search term, leave Replace blank to delete matches...

Cheers,
David.
--
PriceTapestry.com