Найти в файле значение и изменить его на другое
Вот скрипт парсинга
<?php
class Currency
{
/* Config */
// адрес для обновлений
var $updURL = 'http://www.cbr.ru/scripts/XML_daily.asp';
// путь для сохранения файлов modules/currency/currency/
var $path = 'currency/';
// префикс файлов
var $fPrefix = 'curs_';
// сохранять историю (не удалять старые файлы)?
var $keepHistory = false;
// какие валюты участвуют в статистике?
var $currArray = array('840', '978', '980');
// сравнить курсы со вчерашними?
var $changes = true;
function __construct()
{
$today = date("Y-m-d");
$mydate = explode('-', $today);
// нужно ли обновление?
if (!file_exists($this->path . $this->fPrefix . $today . '.php')) {
// обновляем файл
$currentCurrency = array();
$x = 0;
$upd = simplexml_load_file($this->updURL);
foreach ($upd as $node) {
if (in_array($node->NumCode[0], $this->currArray)) {
$currentCurrency[$x]['Value'] = $node->Value[0];
$currentCurrency[$x]['CharCode'] = $node->CharCode[0];
$x++;
}
}
//запись
$this->save2file($currentCurrency, $today);
if (!$this->keepHistory) {
$ago = date("Y-m-d", mktime(0, 0, 0, $mydate[1], $mydate[2] - 2, $mydate[0]));
if (file_exists($this->path . $this->fPrefix . $ago . '.php')) {
unlink($this->path . $this->fPrefix . $ago . '.php');
}
}
}
//показываем информацию из файла
require_once ($this->path . $this->fPrefix . $today . '.php');
$this->view($currency);
}
function save2file($arr, $today)
{
// запишем новую инфу в файл
$h = fopen($this->path . $this->fPrefix . $today . '.php', "w");
fwrite($h, '<?php $currency = Array(); ');
foreach ($arr as $k => $v)
{
fwrite($h, '$currency[\'' . $v['CharCode'] . '\'] = \'' . $v['Value']=str_replace(',','.',$v['Value']) . '\'; '); // fwrite($h, '$currency[\'' . $v['CharCode'] . '\'] = \'' . $v['Value'] . '\'; ');
}
// fwrite($h, '$currency[\'UAH\'] = \'' . $v['Value']/10 . '\'; ');
fwrite($h, ' ?>');
fclose($h);
}
function view($arr)
{
// сравнение курсов
if ($this->changes) {
$today = date("Y-m-d");
$date = date("d.m.Y");
$mydate = explode('-', $today);
$yesterday = date("Y-m-d", mktime(0, 0, 0, $mydate[1], $mydate[2] - 1, $mydate[0]));
if (file_exists($this->path . $this->fPrefix . $yesterday . '.php')) {
require_once ($this->path . $this->fPrefix . $yesterday . '.php');
foreach ($currency as $key => $value) {
if ($arr[$key] > $currency[$key]) {
$arr['x'][$key]['change'] = 'currency/up.gif';
$arr['x'][$key]['Value'] = $arr[$key];
} elseif ($arr[$key] < $currency[$key]) {
$arr['x'][$key]['change'] = 'currency/down.gif';
$arr['x'][$key]['Value'] = $arr[$key];
} elseif ($arr[$key] == $currency[$key]) {
$arr['x'][$key]['change'] = 'currency/null.gif';
$arr['x'][$key]['Value'] = $arr[$key];
}
}
}
}
if (!is_array($arr['x'])) {
echo '<table width=100%><center><font size=2 color=#009900><small>на ' . $date . '</small></font></center>';
foreach ($arr as $k => $v) {
echo '<tr><td><img src="currency/' . $k . '.png"></td><td>' . $v . '</td></tr>';
}
echo '</table>';
} else {
echo '<table width=100%><center><font size=2 color=#009900><small>на ' . $date . '</small></font></center>';
foreach ($arr['x'] as $k => $v) {
echo '<tr><td><img src="currency/' . $k . '.png"></td><td>' . $v['Value'] . '</td><td><img src="' . $v['change'] . '"></td></tr>';
}
echo '</table>';
}
}
}
$curr = new Currency();
?>
Скрипт создает файлы файл curs_ДАта.php с новыми курсами а старый удаляет с таким содержимым:
<?php $currency = Array(); $currency['USD'] = '46.3379'; $currency['EUR'] = '57.8575'; $currency['UAH'] = '29.2353'; ?>
Изначально курс гривны идет за десять единиц '29.2353'. А нужно чтоб было за одну иресть так 2.9235. Подскажите, как сделать? Спасибо.
Источник: Stack Overflow на русском