我有一些PHP代码,“应该”从亚马逊提取价格。但是,它似乎返回错误,任何人都可以帮助诊断可能导致错误的原因吗?我担心正是这种正则表达式会停止显示数据,但却会遇到preg match函数。
<?php
/* Enter the Amazon Product ISIN */
$amazonISIN = "B0034HDRH4";
/* Grab the content of the HTML web page */
$html = file_get_contents("https://www.amazon.co.uk/dp/$amazonISIN");
/* Clean-up */
$html = str_replace(" ", "", $html);
/* The magical regex for extracting the price */
$regex = '/\<b\>(Prezzo|Precio|Price|Prix Amazon|Preis):?\<\/b\>([^\<]+)/i';
/* Return the price */
if (preg_match($regex, $html, $price)) {
$price = number_format((float)($price[2]/100), 2, '.', '');
echo "The price for amazon.com/dp/$amazonISIN is $price";
} else {
echo "Sorry, the item is out-of-stock on Amazon";
}
?>
此外,如果“亚马逊销售”或“亚马逊实现”包含在页面的#merchant-info部分中,我只需要显示价格的代码?