316 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 316 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s $ptr1 = strpos($page, $find) + strlen($find); if (!$ptr1) return array(FALSE); $ptr2 = strpos($page, '" />', $ptr1); $title = substr($page, $ptr1, $ptr2 - $ptr1); $find = '"><img src="'; $ptr1 = strpos($page, $find) + strlen($find); $ptr2 = strpos($page, '"', $ptr1); $image = substr($page, $ptr1, $ptr2 - $ptr1); return array($title, $image); } Get Amazon Sales Rank Sometimes it can be interesting to know how well a book is doing at Amazon. With this utility you can find out that information from all six worldwide Amazon web sites. Figure 12-5 shows the plug-in being used to look up the sales rank information for the book used in the previous plug-in. About the Plug-in This plug-in takes an ISBN number and the domain of an Amazon web site and then returns the sales rank for that title on that site. Upon success, it returns a number representing the book’s popularity, with 1 being the most popular. Upon failure, for example if the book is not found, or if it doesn’t have a rank, it returns FALSE. It requires the following arguments: • $isbn An ISBN-10 number • $site An Amazon web domain, out of: amazon.com, amazon.ca, amazon.co.uk, amazon.fr, amazon.de, and amazon.co.jp FIGURE 12-5 Using this plug-in, sales rank information has also been returned from Amazon. 94 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 317 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 317 Variables, Arrays, and Functions $url String containing the URL of the Amazon mobile web site (for speed) $find String containing the text to find immediately preceding a Sales Rank $end String containing the text to find immediately following a Sales Rank $page String containing the contents of $url $ptr1 Integer pointer to start of $find $ptr2 Integer pointer to start of $end $temp String containing Sales Rank before removing non-digit characters How It Works This plug-in extracts sales rank information from five of the six Amazon mobile web sites. The sixth site used however, amazon.co.jp, is the main web URL, since its mobile site appears not to provide sales rank information. The plug-in works by loading the default URL, comprising the value in $site into $url, and the HTML immediately following the sales rank details into $end. Then a switch statement is used for the different values of $site. In the case of the three English speaking countries, the sites amazon.com, amazon.ca, and amazon.co.uk set the variable $find to the string Sales Rank:, which is what will be searched for in the web page. The two European web sites, amazon.fr and amazon.de, replace the string with French and German translations of the phrase “sales rank,” while the Japanese web site at amazon.co.jp has a different pre– and post–sales rank data string to search for. With these details prepared, the contents of $url is loaded into $page. If this is unsuccessful, then FALSE is returned. Otherwise, the strpos() function is used to find the first occurrence of $find, the location of which is placed in $ptr1. Again, if it is not found, FALSE is returned. Next, $ptr2 is given the location of the subsequent occurrence of $end and the string in between the two is extracted into $temp, from where any non-digit characters are removed before returning its value. How to Use It To obtain a book’s sales rank at a particular Amazon site, just pass the ISBN and domain to the plug-in like this (which should achieve a similar result to that shown in Figure 12-5): echo PIPHP_GetAmazonSalesRank('007149216X', 'amazon.com'); Or you could combine this plug-in with the previous one, as follows: $isbn = '007149216X'; $result = PIPHP_GetBookFromISBN($isbn); if (!$result) echo "Could not find title for ISBN '$isbn'."; else { echo "<img src='$result[1]' align='left'><b>$result[0]<br>" . "Amazon.com Sales Rank: "; echo PIPHP_GetAmazonSalesRank($isbn, 'amazon.com'); } 318 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 318 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s If you do combine the pair, make sure both plug-ins are pasted into your code, or are otherwise included. The Plug-in function PIPHP_GetAmazonSalesRank($isbn, $site) { $url = "http://www.$site/gp/aw/d.html?pd=1" . "&l=Product%20Details&a=$isbn"; $end = '<br />'; switch(strtolower($site)) { case 'amazon.com': case 'amazon.ca': case 'amazon.co.uk': $find = 'Sales Rank: '; break; case 'amazon.fr': $find = 'ventes Amazon.fr: '; break; case 'amazon.de': $find = 'Verkaufsrang: '; break; case 'amazon.co.jp': $find = '<li id="SalesRank">'; $url = "http://$site/gp/product/$isbn"; $end = '(<a'; break; } $page = file_get_contents($url); if (!strlen($page)) return FALSE; $ptr1 = strpos($page, $find); if (!$ptr1) return FALSE; $ptr2 = strpos($page, $end, $ptr1); $temp = substr($page, $ptr1, $ptr2 - $ptr1); return trim(preg_replace('/[^\d]/', '', $temp)); } Pattern Match Word Having a dictionary of 80,000 words at hand, it’s a shame not to do more with it, so this and the next plug-in provide more word-related features. This plug-in will be of use in crossword- or Scrabble game–like scenarios, where you know the number of letters in a word and even have a few letters in place. Given such details, as Figure 12-6 shows, this plug-in will return all possible words in the dictionary that could fit. 95 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 319 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 319 About the Plug-in This plug-in takes a word pattern and then returns a two-element array in which the first is the number of matching words found and the second is an array of the words themselves. On failure, it returns a single-element array with the value FALSE. It requires the following arguments: • $word A word pattern comprising letters and periods (for unknowns) • $dictionary The path to a file of words Variables, Arrays, and Functions $dict String containing the contents of $dictionary $matches[0] Array containing all the matching words How It Works This plug-in takes advantage of PHP’s built-in regular expression handling by loading the dictionary file in $dictionary into $dict and then matching its contents against the pattern supplied in $word. If $dict has no value, the dictionary wasn’t found and so a single-element array with the value FALSE is returned. Otherwise, the dictionary contents are loaded into $dict. The dictionary file should contain words separated by non-word characters or sequences of characters. If you use \n or \r\n pairs as separators (as in the supplied dictionary.txt file on the www.pluginphp.com web site), then the file can be loaded into and edited by most program and text editors. Before performing the matching, the contents of $word are processed with the preg_ replace() function to remove any non-alphabetic or period characters and to convert the entire string to lowercase using the strtolower() function. FIGURE 12-6 The plug-in has found six matches for the pattern s.e.t.ng. 320 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 320 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s A call to the preg_match_all() function is then made, passing the value in $match, surrounded by \b metacharacters to indicate word boundaries. Any and all matches made are then placed into the array in $matches[0]. A two-element array is then returned containing the number of matches found, and the matches themselves. How to Use It To use this plug-in, pass it a pattern to match and the path to a file of words, like this: $result = PIPHP_PatternMatchWord('S.e.t.ng', 'dictionary.txt'); if ($result[0] != FALSE) { echo "Matches for <font face='Courier New'>" . "'$word'</font>:<br><ul>"; foreach ($result[1] as $match) echo "<li>$match</li>"; } In this example, as long as $result[0] isn’t FALSE, then some matches were made, so a foreach loop iterates through them all in $result[1], displaying them as list elements within an unsorted list, but you could use these words in drop-down lists, with checkboxes, or in a variety of other ways. If you download the file plug-ins.zip from the companion web site at www.pluginphp.com, you will find a copy of the dictionary.txt file in the folder entitled 12. The Plug-in function PIPHP_PatternMatchWord($word, $dictionary) { $dict = @file_get_contents($dictionary); if (!strlen($dict)) return array(FALSE); $word = preg_replace('/[^a-z\.]/', '', strtolower($word)); preg_match_all('/\b' . $word . '\b/', $dict, $matches); return array(count($matches[0]), $matches[0]); } Suggest Spelling In Chapter 3, plug-in 8, I introduced a simple spelling checker. Well here’s a companion plug-in you could use with it to actually offer suggested replacements for misspelled words. As Figure 12-7 shows, using the same dictionary of words, this plug-in attempts to find the closest matches to a word it is passed, and returns them in order of likelihood. About the Plug-in This plug-in takes a word that has been unrecognized and returns the closest matches to it. Upon success, it returns a two-element array, the first of which contains the number of words returned, while the second contains an array of words. On failure, it returns a single-element array with the value FALSE. It requires the following arguments: • $word A word • $dictionary The path to a file of words 96 . Functions $url String containing the URL of the Amazon mobile web site (for speed) $find String containing the text to find immediately preceding a Sales Rank $end String containing the text to find immediately. following a Sales Rank $page String containing the contents of $url $ptr1 Integer pointer to start of $find $ptr2 Integer pointer to start of $end $temp String containing Sales Rank before removing. String containing the contents of $dictionary $matches[0] Array containing all the matching words How It Works This plug- in takes advantage of PHP s built -in regular expression handling by loading