Advertisement
zero50x

Внутр. перелинковка

Apr 27th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.12 KB | None | 0 0
  1. error_reporting(E_ALL | E_STRICT);
  2. ini_set('display_errors', TRUE);
  3. ini_set('display_startup_errors', TRUE);
  4. header('Content-type: text/html; charset=windows-1251');
  5.  
  6. // в какой директории файлы
  7. $dir    = 'html';
  8. $files0 = array();
  9. $files1 = scandir($dir);
  10.  
  11. // сбрасываем 2 первых значения
  12. unset($files1[0]);
  13. unset($files1[1]);
  14.  
  15. // делаем нумерацию с нуля
  16. $result = array_merge($files0, $files1);
  17. $files0 = null;
  18. $files1 = null;
  19.  
  20. print_r($result);
  21.  
  22. $countfiles = count($files1);
  23. $numfiles = $countfiles-1;
  24.  
  25. echo "Файлов (эл-в массива): $numfiles <br><br><br>\n\n\n";
  26.  
  27. // адрес сайта
  28. $url = "http://mysite.ru/ppage/";
  29. //$i = 0;
  30.  
  31. // цикл перебора страниц
  32. for ($i = 0; $i < $countfiles; $i++) {
  33.  
  34. $i1 = $i+1;
  35. $i2 = $i+2;
  36. $i3 = $i+3;
  37. $i4 = $i+4;
  38. $i5 = $i+5;
  39.  
  40. // задаём на случай если элементов массива нет (конец папки)
  41. $link1 = ""; $link2 = ""; $link3 = ""; $link4 = ""; $link5 = "";
  42.  
  43. ############################# ИСХОДНЫЙ ФАЙЛ ##########################################
  44.  
  45. // Это первый файл над которым будем работать
  46. $filename = "html/$result[$i]";
  47. $handle = fopen("$filename", "r");
  48. $contents = fread($handle, filesize($filename));
  49. //echo "$contents";
  50.  
  51. $match_a = '/<title>(.+?)<\/title>/ism';
  52. $run_title = preg_match_all($match_a, $contents, $i_a);
  53. echo "{$i_a[1][0]}";
  54. fclose($handle);
  55.  
  56. // Это отсальные файлы из них пока берём только title для ссылок
  57. ############################# ИСХОДНЫЙ ФАЙЛ +1 ##########################################
  58. if(isset($result[$i1])){
  59.     $filename1 = "html/$result[$i1]";
  60.     $handle1 = fopen("$filename1", "r");
  61.     $contents1 = fread($handle1, filesize($filename1));
  62.     $match_a1 = '/<title>(.+?)<\/title>/ism';
  63.     $run_title1 = preg_match_all($match_a1, $contents1, $i_a1);
  64.     echo "{$i_a1[1][0]}";
  65.     $link1 = "<a href=\"{$url}{$result[$i1]}\">{$i_a1[1][0]}</a><br>";
  66.     fclose($handle1);
  67.     $contents1 = null; $run_title1 = null; $i_a1 = null;
  68. }
  69. ############################# ИСХОДНЫЙ ФАЙЛ +2 ##########################################
  70. if(isset($result[$i2])){
  71.     $filename2 = "html/$result[$i2]";
  72.     $handle2 = fopen("$filename2", "r");
  73.     $contents2 = fread($handle2, filesize($filename2));
  74.     $match_a2 = '/<title>(.+?)<\/title>/ism';
  75.     $run_title2 = preg_match_all($match_a2, $contents2, $i_a2);
  76.     echo "{$i_a2[1][0]}";
  77.     $link2 = "<a href=\"{$url}{$result[$i2]}\">{$i_a2[1][0]}</a><br>";
  78.     fclose($handle2);
  79.     $contents2 = null; $run_title2 = null; $i_a2 = null;
  80. }
  81. ############################# ИСХОДНЫЙ ФАЙЛ +3 ##########################################
  82. if(isset($result[$i3])){
  83.     $filename3 = "html/$result[$i3]";
  84.     $handle3 = fopen("$filename3", "r");
  85.     $contents3 = fread($handle3, filesize($filename3));
  86.     $match_a3 = '/<title>(.+?)<\/title>/ism';
  87.     $run_title3 = preg_match_all($match_a3, $contents3, $i_a3);
  88.     echo "{$i_a3[1][0]}";
  89.     $link3 = "<a href=\"{$url}{$result[$i3]}\">{$i_a3[1][0]}</a><br>";
  90.     fclose($handle3);
  91.     $contents3 = null; $run_title3 = null; $i_a3 = null;
  92. }
  93. ############################# ИСХОДНЫЙ ФАЙЛ +4 ##########################################
  94. if(isset($result[$i4])){
  95.     $filename4 = "html/$result[$i4]";
  96.     $handle4 = fopen("$filename4", "r");
  97.     $contents4 = fread($handle4, filesize($filename4));
  98.     $match_a4 = '/<title>(.+?)<\/title>/ism';
  99.     $run_title4 = preg_match_all($match_a4, $contents4, $i_a4);
  100.     echo "{$i_a4[1][0]}";
  101.     $link4 = "<a href=\"{$url}{$result[$i4]}\">{$i_a4[1][0]}</a><br>";
  102.     fclose($handle4);
  103.     $contents4 = null; $run_title4 = null; $i_a4 = null;
  104. }
  105. ############################# ИСХОДНЫЙ ФАЙЛ +5 ##########################################
  106. if(isset($result[$i5])){
  107.     $filename5 = "html/$result[$i5]";
  108.     $handle5 = fopen("$filename5", "r");
  109.     $contents5 = fread($handle5, filesize($filename5));
  110.     $match_a5 = '/<title>(.+?)<\/title>/ism';
  111.     $run_title5 = preg_match_all($match_a5, $contents5, $i_a5);
  112.     echo "{$i_a5[1][0]}";
  113.     $link5 = "<a href=\"{$url}{$result[$i5]}\">{$i_a5[1][0]}</a><br>";
  114.     fclose($handle5);
  115.     $contents5 = null; $run_title5 = null; $i_a5 = null;
  116. }
  117. ############################### КОНЕЦ ##################################################
  118.  
  119. //Заменяем заготовленную кодовую фразу на ссылки
  120. $contents = preg_replace('/<!--\sLINK\s-->/i', "{$link1}\n{$link2}\n{$link3}\n{$link4}\n{$link5}\n", $contents);
  121. echo "$contents";
  122.  
  123. //Записываем в файл и закрываем его
  124. $handle = fopen("$filename", "w");
  125.                 if (fwrite($handle, $contents) === FALSE) {
  126.                     echo "Не могу произвести запись в файл ($filename)<br>";
  127.                     exit;
  128.                 }
  129.                 echo "Данные успешно записаны в файл ($filename)<br>";
  130. fclose($handle);
  131. $contents = null;
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement