I know I haven’t been writing very many posts these days, but my excuse is I’ve been hard at work on some sweet new features. I don’t want to spoil the surprise, but I will give you a hint.

This week, “M.” in San Francisco, CA, writes:

Dear Feed Doctor,
My description field is too long for most of the comparison shopping engines. What should I do?

This is a pretty common problem, M. Most of the engines have some kind of limit on the length of every field you send. The description is the longest, of course, and most of the engines give you several thousand characters. Some, however, only give you a few hundred. How can you make sure your description data isn’t too long?

As you might have guessed, ShoppingAdvisor has a function called LEFT that will truncate text to meet a length restriction. It has two parameters:

  • The text you want to truncate
  • The maximum length you want the output to be

Let’s say that M. needs to shorten her descriptions down to 15 characters (I know that’s awfully small, but it’s just an example). She would write a business rule like this:
LEFT($description,15)
Here’s some sample results:

Text Input Text Output
This is the end This is the end
This is the beginning This is the beg

Notice a couple of things. First, if your input text is shorter than (or the same length as) the maximum length, then nothing happens to it. Also remember that spaces and punctuation count, too. Notice something else: LEFT will chop your text off in the middle of a word. How rude! While this will technically make your feed meet the engines’ specification, having chopped-off words might not look so nice.

What might be nicer is to replace the chopped-off word with an ellipsis. The easiest way I can think of to do that is to truncate the text with TRIM, as we just did, only with a slightly shorter length, and then use our old friend REGEXREPLACE to remove the final word (or partial word) and put in an ellipsis. Here’s M’s updated rule:
REGEXREPLACE(LEFT($description,12),"\s[^\s]*$","...")

Notice that we changed the 15 to 12. This is to guarantee us some room for the ellipsis (three dots, see?). The regular expression says to look for a space followed by zero or more non-space characters at the end of the text. That’s what will get removed and replaced with the ellipsis. Here is the result:

Text Input Text Output
This is the end This is the…
This is the beginning This is the…

Notice that although the first description (”This is the end”) is only 15 characters, and is therefore not too long to send, our rule chops the ” end” off anyway. Remember, too, that although LEFT doesn’t affect text that’s shorter than the maximum length, the REGEXREPLACE doesn’t care, so even your descriptions that are shorter than 15 would get the last word replaced with an ellipsis.

So, maybe we better check the description length first, and if it’s ok, just use the description, and only do the chopping if it’s too long. Here’s M’s final rule:
IF(LENGTH($description)>15,REGEXREPLACE(LEFT($description,12),"\s[^\s]*$","..."),$description)

Ok, that’s all for this week, and probably this month and therefore this year. See you in 2008!