PHP Developer Channel :: 討論園地

您尚未登入。

#1 2005-03-21 17:01:51

gaisen
New member
註冊日期: 2005-03-21
文章數: 6

搜尋天氣報告的程式

各位好:

我本身是php的菜鳥。

我從網上得到:
<?
$url='http://www.hko.gov.hk/textonly/forecast/ndayc.htm';
$start = 15;
$end = 16;
$file=file($url);
for ($i=$start;$i<$end;$i++)
{
$temp = $file[$i];
$temp = ereg_replace("氣 溫 : ","","$temp");
$temp = ereg_replace(" ","","$temp");
$temp = ereg_replace(" ","","$temp");
}
echo $temp;
?>

但不知為何,在加到網頁時,並沒有預期效果。
煩請各位大人告知該如何辦。
謝!

gaisen

ps 會自動顯示香港天氣

最後修改: gaisen (2005-03-21 17:02:53)

離線

 

#2 2005-03-22 09:53:25

Sam Tang
Administrator
來自: Hong Kong
註冊日期: 2005-03-01
文章數: 75
網站

Re: 搜尋天氣報告的程式

先檢視 http://www.hko.gov.hk/textonly/forecast/chinesewx.htm 的原始碼,找出天氣部份的 html 碼,再修改 $startc, $endc, $start, $end 的值。

另外要肯定你的 hosting 允許使用 file() 這類 function 作遠端連接。

離線

 

#3 2005-03-22 10:54:21

gaisen
New member
註冊日期: 2005-03-21
文章數: 6

Re: 搜尋天氣報告的程式

你好呀!
又係我。

已肯定我的web hosting提供此function,因若以*.php為檔,可運行無疑。

但不知為何,當我把程式碼插入html檔或使用include都看不到結果。
如:
一)
<html><body>
<?php
$url='http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';
$start = 15;
$end = 16;
$file=file($url);
for ($i=$start;$i<$end;$i++)
{
$temp = $file[$i];
$temp = ereg_replace("氣 溫 : ","","$temp");
$temp = ereg_replace(" ","","$temp");
$temp = ereg_replace(" ","","$temp");
}
echo $temp; //亦使過用printf("%s", $temp);
?>
</body></html>


二)
<html><body>
<?php
include 'ee.php';
?>
</body></html>

離線

 

#4 2005-03-22 14:13:09

Sam Tang
Administrator
來自: Hong Kong
註冊日期: 2005-03-01
文章數: 75
網站

Re: 搜尋天氣報告的程式

你誤會了我的意思,我不是說是否可以執行 php,而是 "允許使用 file() 這類 function 作遠端連接"。

離線

 

#5 2005-03-22 15:20:15

gaisen
New member
註冊日期: 2005-03-21
文章數: 6

Re: 搜尋天氣報告的程式

對不起,又打攪你了。
是我打錯了,若我上傳以下之code到web hositing,可看到正確之執行結果。但當我加插至html,則不能運作。謝!

<?
$url='http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';
$start = 15;
$end = 16;
$file=file($url);
for ($i=$start;$i<$end;$i++)
{
$temp = $file[$i];
$temp = ereg_replace("氣 溫 : ","","$temp");
$temp = ereg_replace(" ","","$temp");
$temp = ereg_replace(" ","","$temp");
}
echo $temp;
?>

最後修改: gaisen (2005-03-22 15:20:32)

離線

 

#6 2005-03-22 16:22:14

gaisen
New member
註冊日期: 2005-03-21
文章數: 6

Re: 搜尋天氣報告的程式

我己解決問題了。謝!

離線

 

#7 2005-03-22 16:23:04

Sam Tang
Administrator
來自: Hong Kong
註冊日期: 2005-03-01
文章數: 75
網站

Re: 搜尋天氣報告的程式

你插入 html 檔案然不會有內容顯示,因為預設 .htm 及 .html 不會執行 php,這種情況你可以以 javascript 引入 php 檔案或者用 iframe。

建議先看一下入門文章。

離線

 

#8 2005-11-25 01:23:21

btfans
New member
註冊日期: 2005-11-25
文章數: 4

Re: 搜尋天氣報告的程式

Mine:

程式碼:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Get Weather</title>
</head>
<body>
đ ΰ Ӹ ǩ 
<hr><br>

<?

    $file = "http://www.weather.gov.hk/textonly/forecast/chinesewx.htm";
    $contents = file($file);
    $size = sizeof($contents);
    $alldata=implode("\n", $contents); 
    preg_match_all("|<pre.*?>(.*?)</pre>|ism",$alldata,$matches); 
    foreach($matches[1] as $match) 
{    $pieces = explode("\n", $match);
    echo "$pieces[0] <br>";
/*    echo "$pieces[1] <br>";  */
    echo "$pieces[2] <br>";
/*    echo "$pieces[3] <br>"; */
    echo "$pieces[4] <br>";
/*    echo "$pieces[5] <br>"; */
    echo "$pieces[6] <br>";
/*    echo "$pieces[7] <br>"; */
    $rest = substr("$pieces[8]", 24);
    echo "đ ΰ G $rest <br>"; 
       if(trim($pieces[10]) == "ݐ ʠ ׎ G") { 
        echo "$pieces[12] <br>"; }  
       if(trim($pieces[12]) == "ݐ ʠ ׎ G") { 
        echo "$pieces[14] <br>"; }
       if(trim($pieces[14]) == "ݐ ʠ ׎ G") { 
        echo "$pieces[16] <br>"; }  
       if(trim($pieces[16]) == "ݐ ʠ ׎ G") { 
        echo "$pieces[18] <br>"; } 
       if(trim($pieces[18]) == "ݐ ʠ ׎ G") { 
        echo "$pieces[20] <br>"; }                       


/*
  echo nl2br($match)."<br>\n";  */
  }


/*

    for($i = 0; $i < $size; $i++) {
    $alldata .= $contents[$i]; 

    if (preg_match_all("|<pre.*?>(.*?)</pre>|ism",$alldata,$matches));
    {
    $main = implode('\n',$matches[1]);
    echo $main;  

}
    if(trim($alldata) == "ΰ ׅ G") {
            $details = ($contents[$i + 1]);
            echo "ΰ ׅ G$details <br>"; }
    if(trim($alldata) == "# ˗ G") {
            $details = ($contents[$i + 1]);
            echo "# ˗ G $details <br>"; }
    if(trim($alldata) == "đ ΰ ԡ ُG") {
            $details = ($contents[$i + 1]);
            echo "đ ΰG $details <br>"; }
*/

/*

if (preg_match_all('/\/ball_(\d{2,})\.gif/', $alldata, $matches)) 
{ 
  array_walk($matches[1], create_function('&$in', 'if ($in{0} == "0") $in = substr($in, 1);')); 
  
  $bonus    = array_pop($matches[1]); 
  $main     = implode(' ', $matches[1]); 
  $combined = $main . ' + ' . $bonus; 

echo $combined; 
}


}
*/
?>
</body>
</html>

It show like:

天 氣 報 告
--------------------------------------------------------------------------------

程式碼:

上 午 1 時 天 文 台 錄 得: 
氣 溫 : 20 度 
相 對 濕 度 : 百 分 之 81 
天 氣 : 天 色 良 好

離線

 

論壇頁尾

Web Hosting
PHP Developer Channel
Powered by PunBB 1.2.9
© Copyright 2002–2005 Rickard Andersson