You are here:  » Convert Age Range to Individual Values

Active Forum Topics


Convert Age Range to Individual Values

Submitted by ChrisNBC on Mon, 2018-01-22 11:27 in

Hi David,

Happy New Year!...hope you had a great Christmas.

I have been puzzling over a data ‘cleaning’ issue and wondered if you might be able to suggest a solution. In summary, some of my feeds supply age information in the format:

3-4 years,5-6 years,9-10 years,4-5 years,11-12 years,7-8 years

I would like to convert the data into individual values, so for example 3-4 years would become 3 years, 4 years.

I realise I could do this with a whole bunch of search and replace filters, one for each variation but I wondered if you might be able to suggest a neater and more efficient way of doing this change?

Thanks in advance.

Best regards
Chris

Submitted by support on Mon, 2018-01-22 11:50

Hi Chris,

HNY also! Have a go with the following "Age Range" filter, which will separate out the ages or return unchanged if the regexp does not match...

  /*************************************************/
  /* ageRange */
  /*************************************************/
  $filter_names["ageRange"] = "Age Range";
  function filter_ageRangeConfigure($filter_data)
  {
    print "<p>There are no additional configuration parameters for this filter.</p>";
  }
  function filter_ageRangeValidate($filter_data)
  {
  }
  function filter_ageRangeExec($filter_data,$text)
  {
    preg_match("/([0-9]+)\-([0-9]+) years/i",$text,$matches);
    if (count($matches)==3)
    {
      $years = array();
      for($i=$matches[1];$i<=$matches[2];$i++)
      {
        $years[] = $i." years";
      }
      return implode(",",$years);
    }
    else
    {
      return $text;
    }
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2018-01-22 12:41

Hi David,

Thanks for the quick response. I tried the above but noticed the filter only seems to pick up the first instance.

For example in a field containing:

3-4 years,5-6 years,9-10 years,4-5 years,11-12 years,7-8 years

only the 3-4 years is converted.

I wondered if you might be able to suggest a way to force the filter to check the entire field for matches before moving on to the next?

Thanks in advance.

Best regards
Chris

Submitted by support on Mon, 2018-01-22 13:28

Hi Chris,

Have a go with the following for multiple ranges, with duplicate prevention (e.g. 7-8 years, 8-9 years) would return "7 years,8 years,9 years"...

  /*************************************************/
  /* ageRange */
  /*************************************************/
  $filter_names["ageRange"] = "Age Range";
  function filter_ageRangeConfigure($filter_data)
  {
    print "<p>There are no additional configuration parameters for this filter.</p>";
  }
  function filter_ageRangeValidate($filter_data)
  {
  }
  function filter_ageRangeExec($filter_data,$text)
  {
    $years = array();
    $parts = explode(",",$text);
    foreach($parts as $part)
    {
      preg_match("/([0-9]+)\-([0-9]+) years/i",$part,$matches);
      if (count($matches)==3)
      {
        for($i=$matches[1];$i<=$matches[2];$i++)
        {
          $year = $i." years";
          $years[$year] = $year;
        }
      }
    }
    if (count($years))
    {
      return implode(",",$years);
    }
    else
    {
      return $text;
    }
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2018-01-23 11:18

Hi David,

Thanks for your help above, the second version works perfectly.

Best regards
Chris