You are here:  » Find Replace Filters to Truncate a field


Find Replace Filters to Truncate a field

Submitted by ChrisNBC on Fri, 2013-08-30 14:28 in

Hi David,

Please could you tell me if there is a way to use the find and replace filter to find a portion of a field and then replace the contents of the entire field with something else...

For example, I have a feed field which contain something like "Pay Monthly > Apple;". Ideally I would like to use a wildcard search for 'Pay Monthly' and then replace the entire contents of the field with 'Pay Monthly'. Alternatively, could you suggest any other filters which I could use to reach the same result?

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2013-08-30 14:40

Hi Chris,

The "preg Replace All If" filter from this post will do the trick. After adding the code to your includes/filter.php add a new "preg Replace All If" filter to the relevant field, and in the preg Expression box enter:

/Pay Monthly/

...and in the Replace box, just:

Pay Monthly

And then any instance of that field containing "Pay Monthly" will be replaced with just "Pay Monthly". The "/" characters are relevant as the match field is a regular expression (which provides for much greater flexibility than just a single string match). Replace value is just a normal string.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2013-08-30 16:32

Hi David,

Thanks as always for your quick response.

I implemented what you suggested which worked beautifully but I realised that it also deleted the contents of the field when there was no match. Since I need to run filters to pick out two other variations, I implemented the code below (which I took from the linked post):

  /*************************************************/
  /* 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;
    }
  }

It works perfectly, however, I have run into a small problem in that I notice some data fields in the feed contain semi-colons:

e.g Pay as You Go > Sony; Pay Monthly > Sony

This seems to cause the data to be loaded into the wrong fields (i'm guesisng it's because the loader treats the semi-colon as a seperator? even though is's specified as csv format).

I wondered if you could suggest a solution to this issue?

Thanks in advance.

Regards
Chris

Submitted by support on Sat, 2013-08-31 11:17

Hello Chris,

Where that happens it would normally be indicative of a quoted-text format not having been selected on Feed Registration Step 1. Occasionally you come across feeds that only quote fields that contain the separator, and therefore may not have been picked up by the auto-detection logic.

To correct, re-register the feed and in the drop-down box on Feed Registration Step 1 instead of accepting the auto-detected format string manually select the correct format e.g. "Quoted Text - Header Row - Semi-colon Separated" - that should be all it is!

Cheers,
David.
--
PriceTapestry.com