Bu makalede sizlere PHP ile RegEx kullanarak Yahoo'dan hava durumu bilgilerini almayı göstereceğim. Önceki makalemde anlatmış olduğum döviz bilgilerinin aksine hava durumu bilgisi sürekli güncellenen bir bilgi olduğundan bu tarz verileri veritabanında depolamanızı tavsiye etmem.Artık kodumuza geçelim.
function HavaDurumu($sehirkodu,$kucukresim=false) {
$sonuc=array();
$contents=file_get_contents("http://weather.yahoo.com/forecast/".$sehirkodu."_c.html");
$pattern='/<dt>Feels Like:</dt><dd>(d+)°</dd>/';
preg_match($pattern, $contents, $matches);
$sonuc[hissedilen]=$matches[1]." °C";
unset($matches);
$pattern='/<dt>Barometer:</dt><dd>(d+)(sw+)([sw]+)</dd>/';
preg_match($pattern, $contents, $matches);
$sonuc[basinc]=$matches[1].$matches[2];
unset($matches);
$pattern='/<dt>Humidity:</dt><dd>(d+)\%</dd>/';
preg_match($pattern, $contents, $matches);
$sonuc[nem]="%".$matches[1];
unset($matches);
$pattern='/<div class="forecast-icon" style="background:url('(.+?)');/';
preg_match($pattern, $contents, $matches);
if($kucukresim == false) { $sonuc[image]=$matches[1]; } else { $sonuc[image]=substr($matches[1],0,-5)."s.png"; }
unset($matches);
$pattern='/<div class="forecast-temp">s*<h3>(d+)°</h3>s*<p>High:s(d+)°sLow:s(d+)°</p>/';
preg_match($pattern, $contents, $matches);
$sonuc[sicaklik]=$matches[1]." °C";
$sonuc[max]=$matches[2]." °C";
$sonuc[min]=$matches[3]." °C";
unset($matches);
return $sonuc;
}