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!
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
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.