Показать сообщение отдельно
Старый 23.11.2008, 06:38   #2
shmeks
Неактивный пользователь
 
Регистрация: 23.11.2008
Сообщений: 2
Репутация: 1
По умолчанию Re: XML парсер для Gismeteo.ru

Код:
function xml_to_ini($file){
	$contents	= null;
	$item		= 0;
	$open		= file($file);
	foreach($open as $line){
		if(strstr($line, '<item>')) $line = str_replace('<item>', '[item_'.$item++.']', $line);
		if(!strstr($line, '<?xml') and !strstr($line, '<rss') and !strstr($line, 'rss>') and !strstr($line, 'channel>') and !strstr($line, '<link') and !strstr($line, '<category') and !strstr($line, '<enclosure') and !strstr($line, '<source') and !strstr($line, '<guid') and !strstr($line, '</item>')) $contents.= $line;
	}
	list($tmp, $contents)=explode("</image>\r\n", $contents);
	$contents = str_replace('   ', '', $contents);
	$contents = str_replace('  ', '', $contents);
	$contents = str_replace('<title>', 'title = \'', $contents);
	$contents = str_replace('</title>', '\';', $contents);
	$contents = str_replace('<description>', 'value = \'', $contents);
	$contents = str_replace('</description>', '\';', $contents);
	$fp = fopen('base.ini', 'w+');
	$fw = fwrite($fp, $contents);
	fclose($fp);
}

function parse_ini($file){
	$arr=null;
	$open=file($file);
	foreach($open as $line){
		$line=str_replace("\r\n", '', $line);
		if(substr($line, 0, 1)!=';'){
			if(!empty($line)){
				if(preg_match("/\[(.*?)\]/i", $line)){
					$par=preg_replace("'\[(.*?)\]'", '\\1', $line);
				}else{
					if(preg_match("/(.*?)=(.*?);/i", $line)){
						$line=str_replace(' = ', '=', $line);
						$line=str_replace(' =', '=', $line);
						$line=str_replace('= ', '=', $line);
						list($key, $value)=explode('=', $line);
						list($value, $tmp)=explode(';', $value);
						$key=str_replace(' ', '',$key);
						$value=preg_replace("'\'(.*?)\''", '\\1', $value);
						$value=preg_replace("'\"(.*?)\"'", '\\1', $value);
						//$value = preg_replace("' (.*?)'", '\\1', $value);
						if($value=='true') $value = true;
							else
						if($value=='false') $value = false;
						if(empty($par)){
							$arr[$key]=$value;
						}else{
							$arr[$par][$key]=$value;
						}
					}else{
						$arr['error'][]=$line;
					}
				}
			}
		}
	}
	if(empty($arr['error'])){
		return $arr;
	}else{
		return "<pre>В структуре файла конфигурации есть ошибки, проверьте файл $file!</pre>";
	}
}
xml_to_ini('http://informer.gismeteo.ru/rss/26063.xml');
Для вывода погоды используй фун-ю parse_ini:
parse_ini('base.ini');
shmeks вне форума
 
Ответить с цитированием Вверх
Сказали спасибо:
 
Время генерации страницы 0.02181 секунды с 10 запросами