您尚未登入。
各位好:
我本身是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)
離線
先檢視 http://www.hko.gov.hk/textonly/forecast/chinesewx.htm 的原始碼,找出天氣部份的 html 碼,再修改 $startc, $endc, $start, $end 的值。
另外要肯定你的 hosting 允許使用 file() 這類 function 作遠端連接。
離線
你好呀!
又係我。
已肯定我的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>
離線
你誤會了我的意思,我不是說是否可以執行 php,而是 "允許使用 file() 這類 function 作遠端連接"。
離線
對不起,又打攪你了。
是我打錯了,若我上傳以下之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)
離線
你插入 html 檔案然不會有內容顯示,因為預設 .htm 及 .html 不會執行 php,這種情況你可以以 javascript 引入 php 檔案或者用 iframe。
建議先看一下入門文章。
離線
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 天 氣 : 天 色 良 好
離線