Как сделать вывод о результате исполнения? Какие файлы были заменены, а какие не были найдены?
Структура таблицы: http://joxi.ru/v29QWENtZwnLp2
Таблица данных: http://joxi.ru/gmvRqd7SqRVZem
Sql таблица:
CREATE TABLE `files` (
`id` int(11) NOT NULL,
`file_path` mediumtext NOT NULL,
`find_text` mediumtext NOT NULL,
`replace_text` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `files` (`id`, `file_path`, `find_text`, `replace_text`) VALUES
(1, '/template/common/column_left.tpl', '{% if modules %}', '<?php if ($modules) { ?>'),
(2, '/template/common/column_left.tpl', '{% for module in modules %}', '<?php foreach ($modules as $module) { ?>'),
(3, '/template/common/column_left.tpl', '{{ module }}', '<?php echo $module; ?>'),
(4, '/template/common/column_left.tpl', '{% endfor %}', '<?php } ?>'),
(5, '/template/common/column_left.tpl', '{% endif %}', '<?php } ?>'),
(6, '/template/common/column_right.tpl', '{% if modules %}', '<?php if ($modules) { ?>'),
(7, '/template/common/column_right.tpl', '{% for module in modules %}', '<?php foreach ($modules as $module) { ?>'),
(8, '/template/common/column_right.tpl', '{{ module }}', '<?php echo $module; ?>'),
(9, '/template/common/column_right.tpl', '{% endfor %}', '<?php } ?>'),
(10, '/template/common/column_right.tpl', '{% endif %}', '<?php } ?>'),
(11, '/template/common/home.tpl', '{{ header }}', '<?php echo $header; ?>'),
(12, '/template/common/home.tpl', '{{ column_left }}', '<?php echo $column_left; ?>'),
(13, '/template/common/home.tpl', '{% if (column_left and column_right) %}', '<?php if ($column_left && $column_right) { ?>'),
(14, '/template/common/home.tpl', '{% set class = \'col-sm-6\' %}', '<?php $class = \'col-sm-6\'; ?>');
ALTER TABLE `files`
ADD PRIMARY KEY (`id`);
ALTER TABLE `files`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=25;
COMMIT;
<?php
$mysqli = new mysqli("localhost", "root", "", "find_and_replace");
$root_dir = dirname(__FILE__);
function replace_string_in_file($filename, $string_to_replace, $replace_with){
$content=file_get_contents($root_dir . $filename);
$content_chunks=explode($string_to_replace, $content);
$content=implode($replace_with, $content_chunks);
file_put_contents($root_dir . $filename, $content);
}
if ($result = $mysqli->query("SELECT `file_path`, `find_text`, `replace_text` FROM `files`")) {
while ($row = $result->fetch_assoc()) {
$filename = $root_dir . $row['file_path'];
$string_to_replace = $row['find_text'];
$replace_with = $row['replace_text'];
replace_string_in_file($filename, $string_to_replace, $replace_with);
}
$result->close();
}