我有一个包含
HTML代码的页面,如下所示:
-
ABC 70106:10
07:15MYR 50.00Array -
ABC 70106:10
07:15MYR 50.00Array我想从上面的代码中搜索和提取火车号码,训练时间和火车价格.
我的代码没有刮掉我想要的信息,但给了我空白.我查过很多以前发过的问题,但是找不到类似的东西.
我的代码:
$train_doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html)){
$train_doc->loadHTML($html);
libxml_clear_errors();
$train_xpath = new DOMXPath($train_doc);
$train_list = array();
$train = $train_xpath->query('//div[@class="smallFont farelist no-discount"]');
var_dump($train);
if($train->length > 0){
foreach($train as $pat){
$name = $train_xpath->query('div[@class="train-no"]', $pat)->item(0)->nodeValue;
$train_types = array();
$types = $train_xpath->query('div[@class="train-time"]/a', $pat);
foreach($types as $type){
$train_types[] = $type->nodeValue;
$train_list[] = array('name' => $name, 'types' => $train_types);
}
}
}
echo "
";
print_r($train_list);
echo "
";