从html文件获取数据并使用php打印
我的设想是从html文件获取数据并使用php打印,php,Php,我的设想是 使用php读取html页面内容 使用php使用DOM解析器概念从html检索所需数据 在同一php页面中显示检索到的数据(内容与html页面中的内容类似) 我已经完成了所有3个步骤,但在显示数据时,我没有获得所需的格式, 以下是我的全部代码(html、php): Html: 但是,我想用这种格式打印结果 Date:25-08-2011 City:A, Temp:30 c City:B, Temp:29 c Date:26-08-2011 City:A, Temp:28 c City:
Date:25-08-2011
City:A, Temp:30 c
City:B, Temp:29 c
Date:26-08-2011
City:A, Temp:28 c
City:B, Temp:28 c
请任何人帮帮我
谢谢,
Nandha您的问题您正在为每个阵列中的每个元素打印循环。它一直持续到最长的数组,因此每次都会显示日期、城市和温度。您需要重写逻辑。我已经为你做了:) 结果将是这样的:
Array
(
[25-08-2011] => Array
(
[A] => 30 c
[B] => 29 c
)
[26-08-2011] => Array
(
[A] => 28 c
[B] => 28 c
)
)
试试这个:
<?php
include('simple_html_dom.php');
$html = file_get_html('data.html');
foreach($html->find('td') as $e)
{
if ($e->class == 'date')
echo 'Date:'.$e->innertext.'<br>';
else if ($e->class == 'city')
echo 'City:'.$e->innertext;
else if ($e->class == 'temp')
echo 'Temp:'.$e->innertext.'<br>';
}
?>
您的基本逻辑有误。你有多少编码经验?嘿,你会不会是从加拿大来滑铁卢大学学习的?不,你可能把我错当成别人了,对不起。
Date:25-08-2011
City:A, Temp:30 c
City:B, Temp:29 c
Date:26-08-2011
City:A, Temp:28 c
City:B, Temp:28 c
$tree = array();
foreach ($html->find('td') as $e) {
if ($e->class == 'date') {
$date = $e->innertext;
}
if ($e->class == 'city') {
$city = $e->innertext;
}
if ($e->class == 'temp') {
$tree[$date][$city] = $e->innertext;
}
}
Array
(
[25-08-2011] => Array
(
[A] => 30 c
[B] => 29 c
)
[26-08-2011] => Array
(
[A] => 28 c
[B] => 28 c
)
)
<?php
include('simple_html_dom.php');
$html = file_get_html('data.html');
foreach($html->find('td') as $e)
{
if ($e->class == 'date')
echo 'Date:'.$e->innertext.'<br>';
else if ($e->class == 'city')
echo 'City:'.$e->innertext;
else if ($e->class == 'temp')
echo 'Temp:'.$e->innertext.'<br>';
}
?>