You are here:  » Strip Name up to Character


Strip Name up to Character

Submitted by babyuniverse on Thu, 2020-07-16 05:46 in

Hi David,

Hope you are well

One of the feeds I am using has changed product names to the following

adidas - boots
adidas - Mens jacket
adidas - Top
nike - Boots yellow

The brand is the first section up to the "-" followed by the name of the item.
Is there a way to strip put the brand and then the title as 2 fields.

Thanks
Richard

Submitted by support on Thu, 2020-07-16 06:39

Hi Richard,

Sure - the Explode filter will do just that. Use " - " (without the quotes) as the Explode Character or String, and a Return Index of 0 will return the brand part and 1 will return the actual product name (so you could register the same field as both Product Name and Brand and then add an Explode filter to each configured as required)...

Cheers,
David.
--
PriceTapestry.com

Submitted by babyuniverse on Tue, 2020-07-21 23:46

Thanks David,

I just realised that the name contains 2 x "-"

innisfree - Vivid Slim Fit Tint - 1 Ice Peach Black Tea
The first is the brand, the second is part of the product name

Any suggestions
Richard

Submitted by support on Wed, 2020-07-22 06:36

Hi Richard,

Sure - PHP's explode() functions supports a limit parameter, but rather than modify the existing filter as it may already be in use here's the code for a new "Split" filter to add to includes/filter.php:

  /*************************************************/
  /* Split */
  /*************************************************/
  $filter_names["split"] = "Split";
  function filter_splitConfigure($filter_data)
  {
    widget_textBox("Split character or string","text",TRUE,$filter_data["text"],"",3);
    widget_textBox("Return index","index",TRUE,$filter_data["index"],"",2);
    widget_textBox("Limit","limit",FALSE,$filter_data["limit"],"",2);
  }
  function filter_splitValidate($filter_data)
  {
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
    if (!is_numeric($filter_data["index"]))
    {
      widget_errorSet("index","required numeric field");
    }
  }
  function filter_splitExec($filter_data,$text)
  {
    $parts = ($filter_data["limit"]?explode($filter_data["text"],$text,$filter_data["limit"]):explode($filter_data["text"],$text));
    $index = intval($filter_data["index"]);
    if ($index < 0)
    {
      $index = count($parts) + $index;
    }
    return $parts[$index];
  }

Use in place of the Explode filter in this case, and set the limit parameter on the configuration page to 2 and with that in place, a return index of 0 will will return the brand part, and a return index of 1 will return the rest of the string after the first " - "...

Cheers,
David.
--
PriceTapestry.com