Войти

Показать полную графическую версию : PHP - Добавить в парсер замену url


leonid.poydolov@fb
02-12-2014, 16:09
Подскажите в какую строку подставить "str_replace" чтобы подменить "yandex.st/weather/1.2.83/i" на "localhost" ?

$content = str_replace("yandex.st/weather/1.2.83/i", "localhost", $content);

<?php
$url = 'http://pogoda.yandex.ru/moscow/details'; //загрузит

$beginBlock = '<div class=\"b-navigation-city\">';
$endBlock = '<div class=\"b-print b-noprint\">';


$cachefile = dirname(__FILE__).'/cache/weather.php'; // файл кеша

$cache_time_config=7200; //время актуальности кеша 2 часа
//Проверяем наличие кеша
if(file_exists($cachefile)){
$cachetime = filemtime($cachefile); // время создание кеша
if ((time() - $cachetime) < $cache_time_config) { //если кеш не устарел - берем значение из него
$cache_used=true;
include $cachefile;
}
}

if(!$cache_used) // если не использовали кеш - заправшиваем страницу с сервера погоды
{
$pattern = '%' . $beginBlock . '(.*)' . $endBlock . '%isU';

$fp = @fopen($url, 'r');
if (!$fp) exit();
$data = '';
while(!feof($fp))
{
$data .= fgets($fp, 8192);
}
fclose($fp);
if (!preg_match($pattern, $data, $matches)) // страница не доступна
{
if(file_exists($cachefile)){ // берем из устаревшего кеша данные
include $cachefile;
}
else // кеш отсутствует
{
echo 'Сервер погоды временно недоступен';
}
}
else // сохраняем в кеш новое значение
{

file_put_contents($cachefile,'<?php $matches='.var_export($matches,1).'; ?>');
}
}

echo isset($matches[1]) ? $matches[1] : '';
?>

Habetdin
02-12-2014, 16:38
leonid.poydolov@fb, делайте замену после получения данных:
<?php
$url = 'http://pogoda.yandex.ru/moscow/details'; //загрузит
$beginBlock = '<div class=\"b-navigation-city\">';
$endBlock = '<div class=\"b-print b-noprint\">';
$cachefile = dirname(__FILE__) . '/cache/weather.php'; // файл кеша
$cache_time_config = 7200; //время актуальности кеша 2 часа
//Проверяем наличие кеша
if (file_exists($cachefile))
{
$cachetime = filemtime($cachefile); // время создание кеша
if ((time() - $cachetime) < $cache_time_config) //если кеш не устарел - берем значение из него
{
$cache_used = true;
include $cachefile;
}
}
if (!$cache_used) // если не использовали кеш - заправшиваем страницу с сервера погоды
{
$pattern = '%' . $beginBlock . '(.*)' . $endBlock . '%isU';
$fp = @fopen($url, 'r');
if (!$fp)
exit();
$data = '';
while (!feof($fp))
{
$data .= fgets($fp, 8192);
}
fclose($fp);
$data = str_replace("yandex.st/weather/1.2.83/i", "localhost", $data);
if (!preg_match($pattern, $data, $matches)) // страница не доступна
{
if (file_exists($cachefile)) // берем из устаревшего кеша данные
{
include $cachefile;
}
else // кеш отсутствует
{
echo 'Сервер погоды временно недоступен';
}
}
else // сохраняем в кеш новое значение
{
file_put_contents($cachefile, '<?php $matches=' . var_export($matches, 1) . '; ?>');
}
}
echo isset($matches[1]) ? $matches[1] : '';




© OSzone.net 2001-2012