O'Reilly logo

Amazon Hacks by Paul Bausch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Make Product Titles Shorter

There are tools for cutting and slicing strings in every programming environment. Here are some quick examples of cutting book titles down to size.

The ProductName that Amazon returns is always the full name of the product. It’s good to be accurate, but sometimes you need only part of the title. For example, O’Reilly has a book called Malicious Mobile Code: Virus Protection for Windows (O’Reilly Computer Security), and this is exactly what Amazon sends as the ProductName value. Weighing in at 80 characters, it’s a bit long when Malicious Mobile Code could work just as well.

The Code

By looking at book titles as a template, code can automate shortening the titles. In many cases they follow this pattern:

[short title]:[sub title]([series title])

Using string-dicing functions built into most languages for trimming is easy work.

JavaScript
var title = "Malicious Mobile Code: Virus Protection for Windows";
shortTitle = title.split(":")
alert(shortTitle[0]);
Perl
my $title = "Malicious Mobile Code: Virus Protection for Windows";
@shortTitle = split /:/, $title;
print $shortTitle[0];
VBScript
strTitle = "Malicious Mobile Code: Virus Protection for Windows"
shortTitle = Split(strTitle,":")
Wscript.Echo shortTitle(0)
PHP
$title = "Malicious Mobile Code: Virus Protection for Windows";
$shortTitle = split(":",$title);
echo $shortTitle[0];
Python
import string title = "Malicious Mobile Code: Virus Protection for Windows"; shortTitle = string.split(title,":"); print shortTitle[0]; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required