1

this is my code to extract data from table.

but I want delete links.

and how pieces title and price to array.

<?php

$ch = curl_init ("http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

preg_match('#<table[^>]*>(.+?)</table>#is', $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';

echo  $match ;
echo '</table>';

?>  
Kevin
  • 41,694
  • 12
  • 53
  • 70
amir rasabeh
  • 427
  • 8
  • 16

2 Answers2

3

I suggest use an HTML Parser instead. Use DOMDocument + DOMXpath, no need to install they are built-in already. Example:

$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 should contain:

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
    )
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • How to update page every day with curl of course I Guess whit corn job can be update page .. but I don't know how work with that – amir rasabeh Sep 03 '14 at 20:11
  • please see this question http://stackoverflow.com/questions/25696986/remove-first-or-specific-child-node-xpath – amir rasabeh Sep 06 '14 at 14:12
  • @Ghost thank you for this code but can you help me please with other structure of table ? Here is what i need : [example](http://bulkforme.eu.pn/stack.php). Regards – Peacefull Oct 17 '16 at 11:09
  • @Peacefull the idea is just the same, use curl to get the html, use `DOM` to parse the actual HTML – Kevin Oct 17 '16 at 21:54
  • @Ghost yes i followed your actual code and works great to parse the HTML table but i can't manage it to return the title associate to the values. Can you give me an example please ? – Peacefull Oct 18 '16 at 06:12
  • its really helpful – sradha Sep 15 '18 at 06:07
0

If you want parse some web resource, you can use PHP Simple HTML DOM Parser

If you want to get an table and all links inside table:

$html = file_get_html('http://www.digionline.ir/Allprovince/CategoryProducts/cat=10301');
$table = $html->find('table');
$links = $table->find('a');

echo $table;
Dmitriy.Net
  • 1,476
  • 13
  • 24