You are here:  » Filter to clear field if not matched


Filter to clear field if not matched

Submitted by ChrisNBC on Tue, 2014-01-21 16:00 in

Hi David,

Apologies for all the posts today, I'm nearing completion of my site and am just clearing up loose ends...

I wondered if you could tell me if there are any existing filters which can test a field to see if it contains certain words, ignore the field if they are present and delete the entire contents of the field if they are not?

I have spent most of the afternoon trying to modify the "dropRecordIfNotRegExp" filter but so far can't get it to work in the way I need above and am now wondering if maybe I'm trying to modify the wrong filter?

Thanks in advance.

Regards
Chris

Submitted by support on Tue, 2014-01-21 16:05

Hi Chris,

Hope not too much time wasted - have a look at Scan and Set from this thread...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2014-01-21 17:08

Hi David,

Thanks for your quick response. I initially looked at the scan and set filter but decided it probably wouldn't work for me since I need to ignore the entire field if it contains 'GB' 'MB' or 'Unlimited' and delete the contents of the field if no match is found. I know that I can use the scan and set filter to find exact matches but if I need to find for example a field containing 10GB or 250MB ...if I understand the scan and set filter correctly it will just return the 'GB' or 'MB' content which matches but not the value? Is that correct? I just tried to modify scan and set to do what I wanted but without success..the modification looks like...

{code saved}

It didn't crash the import but neither did it work!...wondered if you could suggest the correct code change?

Thanks in advance.

Regards
Chris

Submitted by support on Wed, 2014-01-22 08:38

Hi Chris,

Ah OK - if i've understood correctly you don't the filter to modify the field at all - just look at least one of a list of keywords (comma separated) and if found, return the field "as is", otherwise delete the contents...

So this is basically similar to Drop Record If Not RegExp, expect instead of setting the drop record flag it just clears the contents of the field - have a go with the following:

  /*************************************************/
  /* clearFieldIfNotRegExp */
  /*************************************************/
  $filter_names["clearFieldIfNotRegExp"] = "Clear Field If Not RegExp";
  function filter_clearFieldIfNotRegExpConfigure($filter_data)
  {
    print "Clear field if field does not match regular expression:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
  }
  function filter_clearFieldIfNotRegExpValidate($filter_data)
  {
  }
  function filter_clearFieldIfNotRegExpExec($filter_data,$text)
  {
    if(!preg_match($filter_data["text"],$text))
    {
      return "";
    }
    return $text;
  }

The usual RegExp format applies, so to scan for at least one of MB, GB it would be:

(MB|GB)

...i.e. a pipe separated list of strings to match, all enclosed in brackets. Take care not to include any superfluous white space. Each sub-expression can actually be a full regexp in its own right if you wanted to match more complex patterns - let me know if you're not sure of course...

Cheers,
David.
--
PriceTapestry.com