<?

function utf8_to_cp1251($s){
        
$out "";
        
$byte2 false;
    for (
$c=0;$c<strlen($s);$c++){
       
$i=ord($s[$c]);
       if (
$i<=127$out.=$s[$c];
           if (
$byte2){
               
$new_c2=($c1&3)*64+($i&63);
               
$new_c1=($c1>>2)&5;
               
$new_i=$new_c1*256+$new_c2;
           if (
$new_i==1025){
               
$out_i=168;
           } else {
               if (
$new_i==1105){
                   
$out_i=184;
               } else {
                   
$out_i=$new_i-848;
               }
           }
           
$out.=chr($out_i);
           
$byte2=false;
           }
       if ((
$i>>5)==6) {
           
$c1=$i;
           
$byte2=true;
       }
    }
    return 
$out;
}

function 
xml2array($xml) {
        
$xmlary = array();
               
        
$reels '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
        
$reattrs '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';

        
preg_match_all($reels$xml$elements);

        foreach (
$elements[1] as $ie => $xx) {
                
$xmlary[$ie]["name"] = $elements[1][$ie];
               
                if (
$attributes trim($elements[2][$ie])) {
                        
preg_match_all($reattrs$attributes$att);
                        foreach (
$att[1] as $ia => $xx)
                                
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
                }

                
$cdend strpos($elements[3][$ie], "<");
                if (
$cdend 0) {
                        
$xmlary[$ie]["text"] = substr($elements[3][$ie], 0$cdend 1);
                }

                if (
preg_match($reels$elements[3][$ie]))
                        
$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
                else if (
$elements[3][$ie]) {
                        
$xmlary[$ie]["text"] = $elements[3][$ie];
                }
        }

        return 
$xmlary;
}

function 
balov($balov) {
    if (
$balov 9$balov substr($balov,-2);
    
$tmp substr($balov,-1);
    if (
$balov 10 $balov 15) return "балов";
    if(
$tmp == 1) return "бал";
    if(
$tmp $tmp 5) return "бала";
    return 
"балов";
}

$handle fopen("http://export.yandex.ru/bar/reginfo.xml""rb");
$contents '';
while (!
feof($handle)) $contents .= fread($handle8192);
fclose($handle);
$contents xml2array(utf8_to_cp1251($contents));

$prob_info = @$contents[0]["elements"][1]["elements"];
 
$probka = !empty($prob_info[1]["text"]) ? $contents[0]["elements"][1]["elements"][1]["text"] : 
    (!empty(
$prob_info[1]["name"]) ? false);

if (
$contents[0]["elements"][0]["elements"][0]["text"] != "Киев"$probka false;
$title $probka !== false $prob_info[5]["text"] : 'Информація не доступна';
$bgcolor $probka !== false $prob_info[2]["text"] : '#e10000';
$color $bgcolor == "yellow" "red" "yellow";


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1251" />
  <title>Пробки и погода от яндекса</title>
  <style type="text/css">
  </style>
</head>
<body>
         <h3 title="<?=$title?>" style="color: <?=$color?>;background-color: <?=$bgcolor?>; width: 400px;">
             <?=($probka !== false)? "Пробки ".$probka." ".balov($probka).". ".$title :'Информація не доступна';?>
         </h3>

        <img src="<?=$contents[0]["elements"][2]["elements"][1]["elements"][8]["text"/* картинка*/?>">

        <a rel="nofollow" target="_blank" href="<?=$contents[0]["elements"][2]["elements"][4]["text"/* ссылка*/?>">
            <?=$contents[0]["elements"][2]["elements"][1]["elements"][2]["elements"][3]["text"/* текст */?>
        </a><br/>
        Сейчас: <?=$contents[0]["elements"][2]["elements"][1]["elements"][7]["text"/* температура сегодня */?><br/>
        Завтра: <?=$contents[0]["elements"][2]["elements"][3]["elements"][0]["text"/* температура завтра*/?><br/>
</body>
</html>

<? unset($contents$handle$prob_info); ?>