Some data loss when using explode in php

时间:2015-06-15 14:28:18

标签: php jquery html explode

I want to parse some data (game names) from an external page : https://www.mol.com/Product/GamesHive

using this code :

<?php
    $url = 'https://www.mol.com/Product/GamesHive';
    $content = file_get_contents($url);
    $first_step = explode('<div class="col-xs-4">', $content);
    $second_step = explode('</div>', $first_step[1]);
    echo $second_step[0];
?>

However some data is lost. The original page has 384 items and my page only has 169 items. What's the problem?

1 个答案:

答案 0 :(得分:0)

Is this what you are trying to achieve?

<?php

$curl = curl_init();

curl_setopt_array($curl,
    array(
        CURLOPT_URL              => 'https://www.mol.com/Product/GamesHive',
        CURLOPT_RETURNTRANSFER   => TRUE,
        CURLOPT_SSL_VERIFYHOST   => 0,
        CURLOPT_SSL_VERIFYPEER   => FALSE
    )
);

$response = curl_exec($curl);

curl_close($curl);

$arr = simplexml_import_dom(@DOMDocument::loadHTML($response))
         ->xpath('//div[@class="caption"]');

foreach ($arr as $val)
{
    echo $val->h5->a . '<br />';   
}

?>