]*id="bookDescription_feature_div"[^>]*>.*?
]*>(.*?)<\/span>/si', $html, $matches)) {
$book['description'] = trim(strip_tags($matches[1]));
}
// Look for other format links (Kindle, Paperback, Hardcover)
if (preg_match_all('/]*>.*?]*href="\/dp\/([A-Z0-9]{10})"[^>]*>.*?]*>(.*?)<\/span>/si', $html, $matches)) {
foreach ($matches[1] as $index => $format_asin) {
$format_name = trim(strip_tags($matches[2][$index]));
if ($format_asin !== $asin && !empty($format_name)) {
$book['formats'][] = [
'asin' => $format_asin,
'format' => $format_name,
'url' => 'https://www.amazon.com/dp/' . $format_asin,
];
}
}
}
return $book;
}
/**
* Get all format ASINs for a book by searching
*
* @param string $title Book title.
* @param string $author Author name.
* @return array Format => ASIN mapping.
*/
public function get_format_asins($title, $author = '') {
$formats = [
'kindle' => '',
'paperback' => '',
'hardcover' => '',
'audio' => '',
];
// Search for the book
$results = $this->search_books($title, $author);
if (isset($results['error']) || empty($results)) {
return $formats;
}
// Match results to formats
foreach ($results as $book) {
$book_title = strtolower($book['title'] ?? '');
$book_format = strtolower($book['format'] ?? '');
// Check if this is the same book (title match)
similar_text(strtolower($title), $book_title, $percent);
if ($percent < 60) {
continue;
}
if (strpos($book_format, 'kindle') !== false || strpos($book_format, 'ebook') !== false) {
$formats['kindle'] = $book['asin'];
} elseif (strpos($book_format, 'paperback') !== false) {
$formats['paperback'] = $book['asin'];
} elseif (strpos($book_format, 'hardcover') !== false) {
$formats['hardcover'] = $book['asin'];
} elseif (strpos($book_format, 'audio') !== false || strpos($book_format, 'audible') !== false) {
$formats['audio'] = $book['asin'];
}
}
return $formats;
}
}