Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- error_reporting(E_ALL | E_STRICT);
- ini_set('display_errors', TRUE);
- ini_set('display_startup_errors', TRUE);
- header('Content-type: text/html; charset=windows-1251');
- // в какой директории файлы
- $dir = 'html';
- $files0 = array();
- $files1 = scandir($dir);
- // сбрасываем 2 первых значения
- unset($files1[0]);
- unset($files1[1]);
- // делаем нумерацию с нуля
- $result = array_merge($files0, $files1);
- $files0 = null;
- $files1 = null;
- print_r($result);
- $countfiles = count($files1);
- $numfiles = $countfiles-1;
- echo "Файлов (эл-в массива): $numfiles <br><br><br>\n\n\n";
- // адрес сайта
- $url = "http://mysite.ru/ppage/";
- //$i = 0;
- // цикл перебора страниц
- for ($i = 0; $i < $countfiles; $i++) {
- $i1 = $i+1;
- $i2 = $i+2;
- $i3 = $i+3;
- $i4 = $i+4;
- $i5 = $i+5;
- // задаём на случай если элементов массива нет (конец папки)
- $link1 = ""; $link2 = ""; $link3 = ""; $link4 = ""; $link5 = "";
- ############################# ИСХОДНЫЙ ФАЙЛ ##########################################
- // Это первый файл над которым будем работать
- $filename = "html/$result[$i]";
- $handle = fopen("$filename", "r");
- $contents = fread($handle, filesize($filename));
- //echo "$contents";
- $match_a = '/<title>(.+?)<\/title>/ism';
- $run_title = preg_match_all($match_a, $contents, $i_a);
- echo "{$i_a[1][0]}";
- fclose($handle);
- // Это отсальные файлы из них пока берём только title для ссылок
- ############################# ИСХОДНЫЙ ФАЙЛ +1 ##########################################
- if(isset($result[$i1])){
- $filename1 = "html/$result[$i1]";
- $handle1 = fopen("$filename1", "r");
- $contents1 = fread($handle1, filesize($filename1));
- $match_a1 = '/<title>(.+?)<\/title>/ism';
- $run_title1 = preg_match_all($match_a1, $contents1, $i_a1);
- echo "{$i_a1[1][0]}";
- $link1 = "<a href=\"{$url}{$result[$i1]}\">{$i_a1[1][0]}</a><br>";
- fclose($handle1);
- $contents1 = null; $run_title1 = null; $i_a1 = null;
- }
- ############################# ИСХОДНЫЙ ФАЙЛ +2 ##########################################
- if(isset($result[$i2])){
- $filename2 = "html/$result[$i2]";
- $handle2 = fopen("$filename2", "r");
- $contents2 = fread($handle2, filesize($filename2));
- $match_a2 = '/<title>(.+?)<\/title>/ism';
- $run_title2 = preg_match_all($match_a2, $contents2, $i_a2);
- echo "{$i_a2[1][0]}";
- $link2 = "<a href=\"{$url}{$result[$i2]}\">{$i_a2[1][0]}</a><br>";
- fclose($handle2);
- $contents2 = null; $run_title2 = null; $i_a2 = null;
- }
- ############################# ИСХОДНЫЙ ФАЙЛ +3 ##########################################
- if(isset($result[$i3])){
- $filename3 = "html/$result[$i3]";
- $handle3 = fopen("$filename3", "r");
- $contents3 = fread($handle3, filesize($filename3));
- $match_a3 = '/<title>(.+?)<\/title>/ism';
- $run_title3 = preg_match_all($match_a3, $contents3, $i_a3);
- echo "{$i_a3[1][0]}";
- $link3 = "<a href=\"{$url}{$result[$i3]}\">{$i_a3[1][0]}</a><br>";
- fclose($handle3);
- $contents3 = null; $run_title3 = null; $i_a3 = null;
- }
- ############################# ИСХОДНЫЙ ФАЙЛ +4 ##########################################
- if(isset($result[$i4])){
- $filename4 = "html/$result[$i4]";
- $handle4 = fopen("$filename4", "r");
- $contents4 = fread($handle4, filesize($filename4));
- $match_a4 = '/<title>(.+?)<\/title>/ism';
- $run_title4 = preg_match_all($match_a4, $contents4, $i_a4);
- echo "{$i_a4[1][0]}";
- $link4 = "<a href=\"{$url}{$result[$i4]}\">{$i_a4[1][0]}</a><br>";
- fclose($handle4);
- $contents4 = null; $run_title4 = null; $i_a4 = null;
- }
- ############################# ИСХОДНЫЙ ФАЙЛ +5 ##########################################
- if(isset($result[$i5])){
- $filename5 = "html/$result[$i5]";
- $handle5 = fopen("$filename5", "r");
- $contents5 = fread($handle5, filesize($filename5));
- $match_a5 = '/<title>(.+?)<\/title>/ism';
- $run_title5 = preg_match_all($match_a5, $contents5, $i_a5);
- echo "{$i_a5[1][0]}";
- $link5 = "<a href=\"{$url}{$result[$i5]}\">{$i_a5[1][0]}</a><br>";
- fclose($handle5);
- $contents5 = null; $run_title5 = null; $i_a5 = null;
- }
- ############################### КОНЕЦ ##################################################
- //Заменяем заготовленную кодовую фразу на ссылки
- $contents = preg_replace('/<!--\sLINK\s-->/i', "{$link1}\n{$link2}\n{$link3}\n{$link4}\n{$link5}\n", $contents);
- echo "$contents";
- //Записываем в файл и закрываем его
- $handle = fopen("$filename", "w");
- if (fwrite($handle, $contents) === FALSE) {
- echo "Не могу произвести запись в файл ($filename)<br>";
- exit;
- }
- echo "Данные успешно записаны в файл ($filename)<br>";
- fclose($handle);
- $contents = null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement