|
<?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样式分离,代码优化中……
效果预览:
|
|