php - How to use simple html DOM for this table?

Solution:

tnx man i use your code but it doesn't work and have many error. this is my complete functions code.because the server is gzip encode i read with curl .

        $url = "http://tsetmc.com/Loader.aspx?Partree=15131T&c=IRO1DMOR0004";
    $curl = curl_get_data($url);
    if(!empty($curl) ){
        $html = str_get_html($curl);
        $xml = simplexml_load_string($html);
        var_dump($xml);
        $data = [];

        // For each <tr>
        foreach ($xml->tr as $row) {

            // check path `<td><div title="">`
            $result = $row->xpath('td/div[@title]');
            if (! empty($result)) {
                foreach ($result as $item) {
                    $data[] = str_replace(',', '', $item['title']);
                }
            }
            else {
                // if not found, check the 2nd <td>
                $result = $row->xpath('td[position()=2]');
                foreach ($result as $item) {
                    $data[] = str_replace(',', '', $item);
                }
            }
        }
        return $data;
    }

Answer

Solution:

You could check if the <div title=""> exist. If true, get that value, else, get the value of the second <td>.

Here is an example using SimpleXML:

$html = <<<HTML
    <tbody>
        <tr class="sh" onclick="ii.ShowShareHolder('21711,IRO1DMOR0004')">
            <td>person</td>
            <td><div class="ltr" title="1,100,000">1 M</div></td>
            <td>2.050</td>
            <td>0</td>
            <td><div class=""></div></td>
        </tr>
        <tr class="sh" onclick="ii.ShowShareHolder('42123,IRO1DMOR0004')">
            <td>person</td>
            <td>953,169</td>
            <td>1.780</td>
            <td>0</td>
            <td><div class=""></div></td>
        </tr>
    </tbody>
HTML;

// parse HTML
$xml = simplexml_load_string($html);

$data = [];

// For each <tr>
foreach ($xml->tr as $row) {

    // if not found, check the 2nd <td>
    $item = $row->children()[1];

    // check if a div with title exists
    if (isset($item->div['title'])) {
        $data[] = str_replace(',', '', $item->div['title']);
    }
    else { // else, take the content
        $data[] = str_replace(',', '', $item);
    }
}

var_dump($data);

Output:

array(2) {
  [0]=>
  string(7) "1100000"
  [1]=>
  string(6) "953169"
}

See the live demo.

Source