我建議使用一個HTML解析器,而不是。使用DOMDocument
+ DOMXpath
,無需安裝它們已內置。例如:
$ch = curl_init ("http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$data = array();
// get all table rows and rows which are not headers
$table_rows = $xpath->query('//table[@id="tbl-all-product-view"]/tr[@class!="rowH"]');
foreach($table_rows as $row => $tr) {
foreach($tr->childNodes as $td) {
$data[$row][] = preg_replace('~[\r\n]+~', '', trim($td->nodeValue));
}
$data[$row] = array_values(array_filter($data[$row]));
}
echo '<pre>';
print_r($data);
$data
應包含以下內容:
Array
(
[0] => Array
(
[0] => AMDA4-3400
[1] => 1,200,000
[2] => 1,200,000
)
[1] => Array
(
[0] => AMDSempron 145
[1] => 860,000
[2] => 910,000
)
什麼是最終的輸出? – Ghost 2014-09-02 07:54:41
提取產品名稱和價格//語言是波斯語 – 2014-09-02 08:05:29
並將所有數據放入數組中? – Ghost 2014-09-02 08:07:41