查看: 3141|回复: 3
收起左侧

[电脑数码] 网页天气代码(PHP版)

[复制链接]
发表于 2007-12-15 11:33:30 | 显示全部楼层 |阅读模式 <
<?php
$region = "黄石";
$html = $filecode=file_get_contents("http://www.baidu.com.cn/s?ie=gb2312&bs=%CC%EC%C6%F8%B2%E9%D1%AF&sr=&z=&cl=3&f=8&wd=$region+%CC%EC%C6%F8&ct=0");//获取连接页面数据
$todayResult = array();
preg_match("/今天:温度(.*)。白天:(.*),夜间:(.*)<\/span>。(.*)<BR>/", $html, $todayResult);    //正则匹配(今天天气)
foreach ($todayResult as &$item)
{
    $item = strip_tags($item);    //去除无用php,html代码
}

$temperature = split(' - ',$todayResult[1]);  //分割温度,实现个性化显示。

$tomorrowResult = array();
preg_match("/明天:温度(.*)。白天:(.*),夜间:(.*)<\/span>。(.*)<\/td><\/tr>/", $html, $tomorrowResult);    //正则匹配(明天天气)
foreach ($tomorrowResult as &$item)
{
    $item = strip_tags($item);    //去除无用php,html代码
}
$weatherIcon = '';  //天气图标(测试版的,这个功能还没加上:yct010) )
$theYear = date('Y');    //获取当天年份
$theMonth = date('m');    //获取当天月份
$theDay = date('d');    //获取当天日期
$theWeek = date('w');    //获取当天星期
//格式化星期
$weekArray = array('日','一','二','三','四','五','六');
$theWeek = $weekArray[(int)$theWeek];
//格式化日期
$theDate = $theYear . '年' . $theMonth . '月' . $theDay . '日 星期' .$theWeek;
?>


<div style="width:300px;border:1px solid #cbcbcb;font-size:12px;color:#666;letter-spacing:1px;">
    <div style="border-bottom:1px solid #cbcbcb;height:20px;line-height:20px;padding:3px;font-weight:bold;"><span>今日天气</span></div>
    <div>
     <div style="text-align:center;margin:3px;"><span style="margin:0 20px 0 0;color:#FE7600;font-weight:bold;"><?php echo($region); ?></span><span><?php echo($theDate); ?></span></div>
  <div style="float:left;"><img src="weatherIcon/CloudyLightPartly.png"></div>
  <div style="margin-top:15px;line-height:20px;">
      <div style="font-weight:bold;">白天:<?php echo($todayResult[2]); ?></div>
   <div style="font-weight:bold;">夜间:<?php echo($todayResult[3]); ?></div>
   <div style="font-weight:bold;font-family:Georgia;">气温:<span style="color:#E30000;"><?php echo($temperature[0]); ?> ~ <span style="color:#009999;"><?php echo($temperature[1]); ?></span></div>
   <div style="color:333;"><?php echo($todayResult[4]); ?></div>
  </div>
</div>
</div>
css样式分离,代码优化中……
效果预览:
tq.jpg
发表于 2007-12-17 18:02:30 | 显示全部楼层 <
顶哈紫!```绝对支持楼主的工作~``
 楼主| 发表于 2007-12-17 22:16:54 | 显示全部楼层 <
:yct054) 谢谢
发表于 2007-12-18 13:10:43 | 显示全部楼层 <
踩死你!~!~!~!~:yct038)
您需要登录后才可以回帖 登录 | 注册 微信登录 手机动态码快速登录

本版积分规则

QQ|Archiver|手机版|小黑屋|删帖指引|阳新新阳网 ( 鄂ICP备19013424号-2|42022202000111 )

GMT+8, 2024-7-5 18:32 , Processed in 0.338580 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.