您尚未登入。
先檢視來源網站的 html 碼,找出需要截取的字段,通常是一個特定行數或者前後有一些析別資料。
用 file() 或 file_get_contents() 取得來源網站內容,及用正規表達式截取所需字段,正規表達式的用法請看本站文章。
離線
希望以下的程式可以幫你~
<?php
$url='http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';
$startc = 14;
$endc = 15;
$filec=file($url);
for ($ic=$startc;$ic<$endc;$ic++) {
$tempc = $filec[$ic];
$tempc = ereg_replace("天 文 台 錄 得:","天文台錄得:","$tempc");
$tempc = ereg_replace(" ","","$tempc");
$tempc = ereg_replace("
","","$tempc");
print " $tempc";
}
$start = 15;
$end = 16;
$file=file($url);
for ($i=$start;$i<$end;$i++) {
$tempa = $file[$i];
$tempa = ereg_replace("氣 溫 : ","氣溫:","$tempa");
$tempa = ereg_replace(" 度","C","$tempa");
$tempa = ereg_replace(" ","","$tempa");
$tempa = ereg_replace("
","","$tempa");
print " $tempa";
}
$startb = 16;
$endb = 17;
$fileb=file($url);
for ($ib=$startb;$ib<$endb;$ib++) {
$tempb = $fileb[$ib];
$tempb = ereg_replace("濕 度 : 百 分 之 ","濕度:","$tempb");
$tempb = ereg_replace(" ","","$tempb");
$tempb = ereg_replace("
","%","$tempb");
print " $tempb";
}
?>離線