You are here:  » Extract first few words during import


Extract first few words during import

Submitted by teezone on Sun, 2008-10-26 04:52 in

Hi David, hope all is well!

I have a new datafeed that doesn't contain the proper product name, however if I extract the first 5 words from the description, it will address my problem (I don't want to wait for the merchant to fix). Is this possible?

Thanks!

Submitted by support on Mon, 2008-10-27 11:42

Hi,

A new "First Words" filter could be used to do this, combined with a Text Before filter in the case of this feed to prepend the description to the useless product name. Here's the new filter - add this to includes/filter.php:

  /*************************************************/
  /* firstWords */
  /*************************************************/
  $filter_names["firstWords"] = "First n Words";
  function filter_firstWordsConfigure($filter_data)
  {
    print "Number of words to include:<br />";
    print "<input type='text' name='n' value='".widget_safe($filter_data["n"])."' />";
    widget_errorGet("n");
  }
  function filter_firstWordsValidate($filter_data)
  {
    if (!$filter_data["n"])
    {
      widget_errorSet("n","required field");
    }
    elseif(!is_numeric($filter_data["n"]))
    {
      widget_errorSet("n","must be numeric");
    }
  }
  function filter_firstWordsExec($filter_data,$text)
  {
    $n = intval($filter_data["n"]);
    $words = explode(" ",$text);
    $new_text = "";
    foreach($words as $word)
    {
      $new_text .= $word . " ";
      $n = $n - 1;
      if (!$n) break;
    }
    return trim($new_text);
  }

So to use this, register the useless product name field anyway; and then after registration go to Filters, and add a "Text Before" filter. Use the placeholder mechanism to include the description as the text before using %DESCRIPTION% , where DESCRIPTION is the name of the description field as shown on the feed registration page.

Then, add the new "First n Words" filter, and enter 5 in the box. Save the filter and re-import and fingers crossed that should do the trick!

Cheers,
David.

Submitted by philstone on Thu, 2014-04-24 00:34

Hi David.

Could this script filter using last 5 words instead?

regards

Phil Stone
www.buy24-7.net

Submitted by support on Thu, 2014-04-24 09:04

Hi Phil,

Sure - here's an equivalent "Last n Words" filter...

  /*************************************************/
  /* lastWords */
  /*************************************************/
  $filter_names["lastWords"] = "Last n Words";
  function filter_lastWordsConfigure($filter_data)
  {
    print "Number of words to include:<br />";
    print "<input type='text' name='n' value='".widget_safe($filter_data["n"])."' />";
    widget_errorGet("n");
  }
  function filter_lastWordsValidate($filter_data)
  {
    if (!$filter_data["n"])
    {
      widget_errorSet("n","required field");
    }
    elseif(!is_numeric($filter_data["n"]))
    {
      widget_errorSet("n","must be numeric");
    }
  }
  function filter_lastWordsExec($filter_data,$text)
  {
    $n = intval($filter_data["n"]);
    $words = explode(" ",$text);
    if ($n > count($words)) $n = count($words);
    $newWords = array();
    for($i=0;$i<$n;$i++)
    {
      array_unshift($newWords,array_pop($words));
    }
    return implode(" ",$newWords);
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by philstone on Thu, 2014-04-24 09:45

Perfect David!

Thanks yet again!

regards

Phil Stone
www.buy24-7.net