You are here:  » using filters


using filters

Submitted by jazzerman on Fri, 2011-01-28 21:04 in

Hi David or anyone who mifght know it.
Im trying to figure out what filter (if one exist) I should use to kill letters or symbols in certain order.
for example:
one of the fields of my advertiser's feed contains:
clothing-dress-gg9-d3230blue
clothing-dress-gg9-d3368blackdenim
clothing-dress-gg9-d3368bluedenim
clothing-dress-gg-d6084blackmulti
clothing-dress-h4-t363zebra
clothing-dress-h6-kd2585black
clothing-dress-hh10-t387ablack

what can I do to get rid of everything that goes after "-dress"?
if there such filter can u describe how it works on this particular example. Thanks.

Submitted by support on Sat, 2011-01-29 09:38

Hi,

Assuming that there may be more keywords infront of the 2 pieces of coded information, what I would do is first add a Search and Replace filter, to Search for "-" and Replace with " " (space). This will leave

keyword keyword keyword coded coded

To remove the last 2 words from this string will require a new filter - and something that may have a more wider application would be a "Words" filter, enabling you to return the first n or words, or if n is negative, everything except the last n words.

Add the following code to your includes/filter.php file, just before the closing PHP tag:

  /*************************************************/
  /* words */
  /*************************************************/
  $filter_names["words"] = "Words";
  function filter_wordsConfigure($filter_data)
  {
    print "Word Count:<br />";
    print "<input type='text' size='40' name='count' value='".widget_safe($filter_data["count"])."' />";
    widget_errorGet("count");
  }
  function filter_wordsValidate($filter_data)
  {
    if (!$filter_data["count"] || !is_numeric($filter_data["count"]))
    {
      widget_errorSet("count","required numeric field");
    }
  }
  function filter_wordsExec($filter_data,$text)
  {
    $newwords = array();
    $words = explode(" ",$text);
    $c = intval($filter_data["count"]);
    if ($c < 0)
    {
      $c = count($words) + $c;
    }
    for($i=0;$i<$c;$i++)
    {
      $newwords[] = $words[$i];
    }
    return implode(" ",$newwords);
  }

So in your case, add a new Words filter (after the Search and Replace), and use -2 as the count value to strip the coded data from the end!

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by jazzerman on Mon, 2011-01-31 20:02

Thanks a lot David

All The Best,

Slava

Submitted by philstone on Thu, 2012-01-12 16:50

Hi David

Trying to get the 'Words' filter working on my site, when i use -1, -2 etc it doens't cut the last words from each product name, when i use it to cut number of words using + it works grand, but i need to remove the last word from each product name in the feed, have you any ideas what may be wrong?

regards

Phil Stone
www.buy24-7.net

Submitted by support on Thu, 2012-01-12 17:23

Hi Phil,

My apologies - the modification above was incorrect for a negative index. This line:

      $c = count($words) - $c;

...should in fact be:

      $c = count($words) + $c;

Also corrected above.

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Sat, 2012-06-23 07:18

Greetings,

Looking to be able to delete the FIRST word on some fields. Can not use Search & Replace as the first word is not always the same.

Using 0 returns this warning:

• required numeric field

Thanks!

Submitted by support on Sat, 2012-06-23 10:39

Hi,

Probably easiest to create a new filter - Trim Words to remove n words from the beginning or end of the string. Have a go with something like this;

{code saved - see updated version below...}

So to remove just the first word, use 1 for Left and leave the Right field empty...

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Mon, 2012-07-09 21:14

Hi David,

Tried using "Trim Words" to remove ONLY the LAST word in a string - received the same "must be numerical" error.

Ideas?

Thanks!

Submitted by support on Tue, 2012-07-10 08:09

Hi,

Sorry about that - the logic was wrong - || (OR) instead of && (AND) - in the filter_trimWordsValidate() function - corrected above...

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Tue, 2012-07-10 17:59

Hi David,

Trying to delete the last word in a string. Using '1' in the 'Right' box and leaving the 'Left' box empty.

No matter what I put in the 'Right' box, it deletes / removes the entire string.

:)

Thanks for your help!

Submitted by support on Wed, 2012-07-11 07:57

Hi,

I'd forgotten to copy across part of the logic required - please use this version:

  /*************************************************/
  /* Trim Words */
  /*************************************************/
  $filter_names["trimWords"] = "Trim Words";
  function filter_trimWordsConfigure($filter_data)
  {
    print "Left:<br />";
    print "<input type='text' size='40' name='left' value='".widget_safe($filter_data["left"])."' />";
    widget_errorGet("left");
    print "Right:<br />";
    print "<input type='text' size='40' name='right' value='".widget_safe($filter_data["right"])."' />";
    widget_errorGet("right");
  }
  function filter_trimWordsValidate($filter_data)
  {
    if ($filter_data["left"] && !is_numeric($filter_data["left"]))
    {
      widget_errorSet("count","must be numeric");
    }
    if ($filter_data["right"] && !is_numeric($filter_data["right"]))
    {
      widget_errorSet("right","must be numeric");
    }
  }
  function filter_trimWordsExec($filter_data,$text)
  {
    $words = explode(" ",trim($text));
    $newwords = array();
    $c = count($words);
    if ($filter_data["right"]) $filter_data["right"] = (($c+1)-$filter_data["right"]);
    for($i=1;$i<=$c;$i++)
    {
      if($filter_data["left"] && ($i <= $filter_data["left"])) continue;
      if($filter_data["right"] && ($i >= $filter_data["right"])) continue;
      $newwords[] = $words[$i-1];
    }
    return implode(" ",$newwords);
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Wed, 2012-07-11 18:48

Hi David,

This is strange:

This merchant's feed, for whatever reason, has two of everything in it. Each line item has a duplicate.

The 'Trim Words' is being used both on the 'Product Name' and 'Product Description' fields as they felt it necessary to add sizes.

When the Slow Import (through admin) is used the first occurrence of the product is filtered correctly, both the 'Product Name' and 'Product Description'. When the filter comes to the second occurrence of the same item only the 'Product Description' is filtered, not the 'Product Name'.

No other filters are being applied to 'Product Name' and 'Product Description'.

Drop Record If Not > Category (Category)
Search and Replace > Category (Category)
Search and Replace > Category (Category)
Explode > Model (ProductID)
Text Before > Model (ProductID)
Trim Words > Product Description (Title)
Trim Words > Product Name (Title)
Text After > Buy URL (BuyURL)

Am stumped.

Thoughts?

Thanks!

Submitted by support on Thu, 2012-07-12 08:44

Hi,

If the filter results in the product name being identical (by removing "size" from the end of the product name as I understand it) under normal circumstances the second instance would then be dropped as a duplicate - could that be what's happening or are you still finding 2 products in the database following import?

Cheers,
David.
--
PriceTapestry.com

Submitted by Convergence on Thu, 2012-07-12 15:22

Hi David,

Their are two in the database after importing - One where the size has been trimmed and one without.

Submitted by support on Fri, 2012-07-13 07:50

Hi,

Could you perhaps post 4 example records from the feed; followed by the product names that are actually imported as a result - that will help me to understand what's happening... Also you mentioned using slow import- does the same problem occur after a normal / command line import?

Thanks!
David.
--
PriceTapestry.com