Advertisement
zero50x

Дорг 1

Jan 14th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.02 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8.  
  9. <?php
  10. //error_reporting(E_ALL | E_STRICT);
  11. ini_set('display_errors', TRUE);
  12. ini_set('display_startup_errors', TRUE);
  13. header('Content-type: text/html; charset=UTF-8');
  14.  
  15. // Соединение с БД
  16.     $link = mysqli_connect('localhost','mytest','qwerr','basat') or die("Error " . mysqli_error($link));
  17.     if(mysqli_connect_errno()) die('Ошибка соединения: '.mysqli_connect_error()); //или if(!$link) {..
  18.     mysqli_set_charset($link, "utf8");
  19.  
  20. // Время
  21.     $g1 = microtime(true);
  22.  
  23. // Таблица с которой работаем
  24.     $source_table = 'xml_list_3_50k_12';
  25.     $time_table1 = "time_table1";
  26.  
  27. # Первый блок с базой
  28.    #### СКОЛЬКО ФРАЗ В БАЗЕ (СКОЛЬКО СТРАНИЦ БУДЕТ СГЕНЕРИРОВАНО) ########################
  29.    
  30.     $res = mysqli_query($link," SELECT COUNT(*) FROM `".$source_table."` ");
  31.     if($res){ //если запрос успешный
  32.     while($row = mysqli_fetch_assoc($res))
  33.         {$data[] = $row;}
  34.     }
  35.     $myc = implode("", $data[0]);
  36.     echo "Всего фраз в таблице: = $myc<br>\n\n";
  37.    
  38.  
  39.    
  40.     #### БЕРЁМ ИЗ БД ФРАЗУ И URL КОНКРЕТНОГО ID ####################################################
  41.    
  42.     mysqli_query($link," LOCK TABLES `".$source_table."` WRITE ") or die (mysqli_errno($link) . ": ошибка лока " . mysqli_error($link). " ошибка лока \n");
  43.     $res = mysqli_query($link," SELECT `id`, `key`, `urls` FROM `".$source_table."`   LIMIT 1 ");
  44.     if($res) { //если запрос успешный
  45.         while($row = mysqli_fetch_assoc($res)) {
  46.             $mytxt[] = $row;
  47.         }  
  48.             //var_dump($mytxt);
  49.             $lastid = $mytxt[0]['id'];
  50.             $arttitle = $mytxt[0]['key'];
  51.            
  52.                 $pieces = explode("|", $mytxt[0]['urls']);
  53.                 //var_dump($pieces);
  54.                 $num_pieces = count($pieces);
  55.                    
  56.                     $ist = array();
  57.                     ### !!!!! ВАЖНО ЧТО БЫ 50 URL НЕ ОБРАБАТЫВАЛОСЬ вместо $num_pieces ставим 20
  58.                    for ($i6 = 0; $i6 < 20; $i6++) {
  59.                         if(strlen($pieces[$i6]) < 1){unset($pieces[$i6]);}
  60.                         else { $ist[] = $pieces[$i6]; }
  61.                     }
  62.                 sort($ist);
  63.                
  64.                 echo "<b>Запрос:</b> $arttitle<br><br>";
  65.  
  66.                 echo "<pre>";
  67.                 var_dump($ist);
  68.                 echo "</pre>";
  69.  
  70.                 echo "<br><br><br>";
  71. ### Удаляю значение
  72.    $zzapros = "DELETE FROM `".$source_table."` WHERE `id`=".$lastid." ";
  73.     mysqli_query($link," $zzapros ") or die (mysqli_errno($link) . ": ошDEL " . mysqli_error($link). "  ошDEL \n");
  74.     mysqli_query($link," UNLOCK TABLES ") or die (mysqli_errno($link) . ": ошибка анлока " . mysqli_error($link). " ошибка анлока \n");
  75.  
  76.         }
  77.        
  78. ############################ ПОГНАЛ САМ CURL #######################################################
  79.  
  80.  
  81. #######################################################################################
  82. #######################################################################################
  83.  
  84. if (isset($ist)) {
  85.     // Отладка
  86.     echo "ist существует<br>";
  87.  
  88. $maintext = "";
  89.     foreach ($ist as $url_link) { // здесь правильно
  90.         $url_link = trim($url_link);
  91.         if ($url_link) {
  92.             // echo "Fetching {$url_link}...<br/>\r\n";
  93.             $main = curl_init($url_link);
  94.             //шлем заголовки
  95.                 curl_setopt($main, CURLOPT_RETURNTRANSFER, 1);
  96.                 curl_setopt($main, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.7.62 Version/11.01');
  97.  
  98.             if ($main) {
  99.  
  100.                 // Выполняем запрос и приводим к одной кодировке
  101.                 $html = curl_exec($main);
  102.                
  103. ######################### Определение кодировки #############################################################
  104.    @$str_cp1251 = iconv('UTF-8', 'Windows-1251', $html);
  105.     $kod1 = preg_match('#.#u', $html);
  106.     $kod2 = preg_match('#.#u', $str_cp1251);
  107.     if ($kod1 == 0) { // если $kod1 равен нулю то это 1251, конверируем её
  108.         $html = iconv("WINDOWS-1251", "UTF-8", $html);
  109.     }
  110.     else { /* если $kod1 не равен нулю то это UTF-8 подробнее о кодировках тут: http://habrahabr.ru/post/107945/#comment_3411725 */ }
  111.  
  112.  
  113. $n_mass = 0;
  114. include 'stop_content_word.php';
  115.  
  116. // забираем title
  117.     $base_title = '/<title>(.*?)<\/title>/ism';
  118.     $pars_title = preg_match_all($base_title, $html, $title);
  119.     $my_title = $title[1][0];
  120.     echo "Title: <b>$my_title</b><br>";
  121.  
  122. ############### ЕСЛИ В TITLE ЕСТЬ ФРАЗА - НЕ ОБРАБАТЫВАЕМ ЭТУ СТРАНИЦУ #########################
  123. //
  124.     $count_t_word = count($title_content_word);  // количество стоп-слов в title берётся из файла stop_content_word
  125.     for ($iw = 0; $iw < $count_t_word; $iw++) {
  126.         $word=$title_content_word[$iw];
  127.         $pos = strpos($my_title, $word);
  128.         if ($pos != false){ $html=""; /* exit("В TITLE обнаружено запрещённое слово, обработка прекращена"); */}
  129.     }
  130.    
  131.  
  132. $text = $html;
  133. $nl = chr(13).chr(10);
  134.  
  135. // вначале избавимся от таблиц (пока их не берём, сэкономим время)
  136. $text = preg_replace('~<table.*?>~is', '<table>', $text);
  137. $text = preg_replace('/<table>(.*?)<\/table>/is', '', $text);
  138.  
  139. // удаление картинок
  140. $text = preg_replace('/<img(.+?)>/is', '', $text);
  141. // удаление ссылок
  142. $text = preg_replace('~<a.*?>~is', '<a>', $text);
  143. // $text = preg_replace('/<a>(.*?)<\/a>/i', '', $text); ссылки мне нужны ещё
  144. $text = preg_replace('/<a><\/a>/i', '', $text);
  145.  
  146. // удаление 2 и более пробелов
  147. $text = preg_replace("/\s{2,}/",' ',$text);
  148. $text = preg_replace('/\s</i', '<', $text);
  149. $text = preg_replace('/>\s/i', '>', $text);
  150. $text = preg_replace('/\s\s</i', '<', $text);
  151. $text = preg_replace('/>\s\s/i', '>', $text);
  152. $text = preg_replace('/\s\s\s</i', '<', $text);
  153. $text = preg_replace('/>\s\s\s/i', '>', $text);
  154. $text = preg_replace('/\n\n\n/i', '\n\n', $text);
  155.  
  156. // переносы <br>
  157. $text = preg_replace('/<br><br>/i', '</p><p>', $text);
  158. $text = preg_replace('/<br\s\/><br\s\/>/i', '</p><p>', $text);
  159. $text = preg_replace('/<br\/><br\/>/i', '</p><p>', $text);
  160.  
  161. $text = preg_replace('~<iframe.*?>~is', '<iframe>', $text);
  162. $text = preg_replace('/<iframe>(.*?)<\/iframe>/is', '', $text);
  163.  
  164. $text = preg_replace('~<b.*?>~is', '<b>', $text);
  165. $text = preg_replace('~<small.*?>~is', '<small>', $text);
  166. $text = preg_replace('~<big.*?>~is', '<big>', $text);
  167. $text = preg_replace('~<p.*?>~is', '<p>', $text);
  168. $text = preg_replace('~<br.*?>~is', '<br>', $text);
  169. $text = preg_replace('~<font.*?>~is', '<font>', $text);
  170. $text = preg_replace('~<div.*?>~is', '<div>', $text);
  171. $text = preg_replace('~<select.*?>~is', '<div>', $text);
  172. $text = preg_replace('~<span.*?>~is', '<span>', $text);
  173. $text = preg_replace('~<ul.*?>~is', '<ul>', $text);
  174. $text = preg_replace('~<h1.*?>~is', '<h1>', $text);
  175. $text = preg_replace('~<h2.*?>~is', '<h2>', $text);
  176. $text = preg_replace('~<h3.*?>~is', '<h3>', $text);
  177. $text = preg_replace('~<h4.*?>~is', '<h4>', $text);
  178. $text = preg_replace('~<h5.*?>~is', '<h5>', $text);
  179. $text = preg_replace('~<h6.*?>~is', '<h6>', $text);
  180.  
  181. // главная замена div и span на Р
  182. $text = preg_replace('/<div>/i', '<p>', $text);
  183. $text = preg_replace('/<\/div>/i', '</p>', $text);
  184.  
  185. $text = preg_replace('/<span>/i', '<p>', $text); // тест замена span на p
  186. $text = preg_replace('/<\/span>/i', '</p>', $text); // тест замена span на p
  187.  
  188. $text = preg_replace('/<font>/i', ' ', $text);
  189. $text = preg_replace('/<\/font>/i', ' ', $text);
  190.  
  191. $text = preg_replace('/<big>/i', '', $text);
  192. $text = preg_replace('/<\/big>/i', '', $text);
  193.  
  194. $text = preg_replace('/<small>/i', '', $text);
  195. $text = preg_replace('/<\/small>/i', '', $text);
  196.  
  197. $text = preg_replace('/<noindex>/i', '', $text);
  198. $text = preg_replace('/<\/noindex>/i', '', $text);
  199.  
  200.  
  201. $text = preg_replace('/<p><\/p>/i', '', $text);
  202. $text = preg_replace('/<li><\/li>/i', '', $text);
  203. $text = preg_replace('/<ul><\/ul>/i', '', $text);
  204. $text = preg_replace('/<ol><\/ol>/i', '', $text);
  205.  
  206. $text = preg_replace('/<script(.*?)>/is', '<script>', $text);
  207. $text = preg_replace('/<script>(.*?)<\/script>/is', '', $text);
  208. $text = preg_replace('/<noscript>(.*?)<\/noscript>/is', '', $text);
  209.  
  210. $text = preg_replace('/<form(.*?)>/is', '<form>', $text);
  211. $text = preg_replace('/<form>(.*?)<\/form>/is', '', $text);
  212.  
  213. $text = preg_replace('/<select(.*?)>/is', '<select>', $text);
  214. $text = preg_replace('/<select>(.*?)<\/select>/is', '', $text);
  215.  
  216. $text = preg_replace('/<nav(.*?)>/is', '<nav>', $text);
  217. $text = preg_replace('/<nav>.*?<\/nav>/is', '', $text);
  218.  
  219. $text = preg_replace('/<style(.*?)>/is', '<style>', $text);
  220. $text = preg_replace('/<style>.*?<\/style>/is', '', $text);
  221.  
  222. $text = preg_replace('/<ins(.*?)>/is', '<ins>', $text);
  223. $text = preg_replace('/<ins>(.*?)<\/ins>/is', '', $text);
  224. $text = preg_replace('/<xml>(.*?)<\/xml>/is', '', $text);
  225. //$text = preg_replace('/<strong>.*?<\/strong>/is', '', $text);
  226.  
  227. $text = preg_replace('/<input(.*?)>/is', '<input>', $text);
  228. $text = preg_replace('/<!(.*?)>/is', '', $text);
  229. $text = preg_replace('/<li(.*?)>/is', '<li>', $text);
  230. $text = preg_replace('/<link(.*?)>/is', '', $text);
  231.  
  232. // em и знаки после неё
  233. $text = preg_replace('/<em><\/em>/i', ' ', $text);
  234. $text = preg_replace('/<\/em><em>/i', ' ', $text);
  235. $text = preg_replace('/<em>/i', ' <em>', $text);
  236. $text = preg_replace('/<\/em>/i', '</em> ', $text);
  237.  
  238. // strong и знаки после неё
  239. $text = preg_replace('/<strong><\/strong>/i', ' ', $text);
  240. $text = preg_replace('/<\/strong><strong>/i', ' ', $text);
  241. $text = preg_replace('/<strong>/i', ' <strong>', $text);
  242. $text = preg_replace('/<\/strong>/i', '</strong> ', $text);
  243.  
  244. // b и знаки после неё
  245. $text = preg_replace('/<b><\/b>/i', ' ', $text);
  246. $text = preg_replace('/<\/b><b>/i', ' ', $text);
  247. $text = preg_replace('/<b>/i', ' <b>', $text);
  248. $text = preg_replace('/<\/b>/i', '</b> ', $text);
  249.  
  250. // i и знаки после неё
  251. $text = preg_replace('/<i><\/i>/i', '', $text);
  252. $text = preg_replace('/<\/i><i>/i', ' ', $text);
  253. $text = preg_replace('/<i>/i', ' <i>', $text);
  254. $text = preg_replace('/<\/i>/i', '</i> ', $text);
  255.  
  256. //$text = preg_replace('/<h3><\/h3>/i', '', $text);
  257. $text = preg_replace('/<p>&raquo;<\/p>/i', '', $text);
  258. $text = preg_replace('/<li><\/li>/i', '', $text);
  259. $text = preg_replace('/<ul><\/ul>/i', '', $text);
  260. $text = preg_replace('/<ol><\/ol>/i', '', $text);
  261. $text = preg_replace('/<p><p>/is', '<p>', $text);
  262. $text = preg_replace('/<\/p><\/p>/is', '</p>', $text);
  263. $text = preg_replace('/<hr\s\/>/i', '', $text);
  264. $text = preg_replace('/<li>&raquo;<\/li>/i', '', $text);
  265. $text = preg_replace('/–<i>«/i', '– <i>«', $text);
  266. $text = preg_replace('/–<b>«/i', '– <b>«', $text);
  267. $text = preg_replace('/–<em>«/i', '– <em>«', $text);
  268.  
  269. // не берёт H-заголовки, обернём в P
  270. $text = preg_replace('/<h1>/i', '<p><h1>', $text);
  271. $text = preg_replace('/<h2>/i', '<p><h2>', $text);
  272. $text = preg_replace('/<h3>/i', '<p><h3>', $text);
  273. $text = preg_replace('/<h4>/i', '<p><h4>', $text);
  274. $text = preg_replace('/<h5>/i', '<p><h5>', $text);
  275. $text = preg_replace('/<h6>/i', '<p><h6>', $text);
  276.  
  277. $text = preg_replace('/<\/h1>/is', '</h1></p>', $text);
  278. $text = preg_replace('/<\/h2>/is', '</h2></p>', $text);
  279. $text = preg_replace('/<\/h3>/is', '</h3></p>', $text);
  280. $text = preg_replace('/<\/h4>/is', '</h4></p>', $text);
  281. $text = preg_replace('/<\/h5>/is', '</h5></p>', $text);
  282. $text = preg_replace('/<\/h6>/is', '</h6></p>', $text);
  283.  
  284. // убираем пробелы перед знаками препинания
  285. $text = preg_replace('/\s\./i', '.', $text);
  286. $text = preg_replace('/\s,/i', ',', $text);
  287. $text = preg_replace('/\s:/i', ':', $text);
  288. $text = preg_replace('/\s;/i', ';', $text);
  289. $text = preg_replace('/\s!/i', '!', $text);
  290. $text = preg_replace('/\s\?/i', '?', $text);
  291.  
  292. // пустые теги
  293. $text = preg_replace('/<p><br\s\/>/i', '<p>', $text);
  294. $text = preg_replace('/<p><br>/i', '<p>', $text);
  295. $text = preg_replace('/<p><\/p>/is', '', $text);
  296. $text = preg_replace('/<p><p>/is', '<p>', $text);
  297. $text = preg_replace('/<p>\s<p>/is', '<p>', $text);
  298. $text = preg_replace('/<b><p>/is', '<p>', $text);
  299. $text = preg_replace('/<b><\/p>/is', '</p>', $text);
  300.  
  301. // отдельные русские слова
  302. $text = preg_replace('/Содержание:/i', '', $text);
  303. $text = preg_replace('/Глава 1./i', '', $text);
  304. $text = preg_replace('/Глава 2./i', '', $text);
  305. $text = preg_replace('/Глава 3./i', '', $text);
  306. $text = preg_replace('/Глава 4./i', '', $text);
  307. $text = preg_replace('/Глава 5./i', '', $text);
  308. $text = preg_replace('/Глава 6./i', '', $text);
  309. $text = preg_replace('/Глава 7./i', '', $text);
  310. $text = preg_replace('/Глава 8./i', '', $text);
  311. $text = preg_replace('/Глава 9./i', '', $text);
  312. $text = preg_replace('/Глава 10./i', '', $text);
  313. $text = preg_replace('/Раздел 1./i', '', $text);
  314. $text = preg_replace('/Раздел 2./i', '', $text);
  315. $text = preg_replace('/Раздел 3./i', '', $text);
  316. $text = preg_replace('/Раздел 4./i', '', $text);
  317. $text = preg_replace('/Раздел 5./i', '', $text);
  318. $text = preg_replace('/Раздел 6./i', '', $text);
  319. $text = preg_replace('/Раздел 7./i', '', $text);
  320. $text = preg_replace('/Раздел 8./i', '', $text);
  321. $text = preg_replace('/Раздел 9./i', '', $text);
  322. $text = preg_replace('/Раздел 10./i', '', $text);
  323.  
  324. // это попытка убрать заголовки без окончания
  325. $text = preg_replace('/<p><h1><p>/i', '<p>', $text);
  326. $text = preg_replace('/<p><h2><p>/i', '<p>', $text);
  327. $text = preg_replace('/<p><h3><p>/i', '<p>', $text);
  328. $text = preg_replace('/<p><h4><p>/i', '<p>', $text);
  329. $text = preg_replace('/<p><h5><p>/i', '<p>', $text);
  330. $text = preg_replace('/<p><h6><p>/i', '<p>', $text);
  331.  
  332. $text = preg_replace('/<\/p><\/h1><\/p>/i', '</p>', $text);
  333. $text = preg_replace('/<\/p><\/h2><\/p>/i', '</p>', $text);
  334. $text = preg_replace('/<\/p><\/h3><\/p>/i', '</p>', $text);
  335. $text = preg_replace('/<\/p><\/h4><\/p>/i', '</p>', $text);
  336. $text = preg_replace('/<\/p><\/h5><\/p>/i', '</p>', $text);
  337. $text = preg_replace('/\/<p><\/h6><\/p>/i', '</p>', $text);
  338. $text_clear_long = strip_tags($text);
  339. $long_text = strlen($text_clear_long);
  340.  
  341. // echo "$text<br>";
  342. echo "Длина текста без тегов всего: $long_text<br>";
  343.  
  344.  
  345. ################### РАЗДЕЛЯЕМ НА <A> ССЫЛКИ И СЧИТАЕМ КОЛИЧЕСТВО ###################
  346.  
  347. // сколько ссылок на странице
  348.     $match_a = '/<\/a>(.+?)<a>/ism';
  349.     $run_title = preg_match_all($match_a, $text, $i_a);
  350.     $count_a = count($i_a[0]); // считаем кол-во элементов массива num
  351.  
  352. // Обнуляем если самый большой кусок текста между ссылками < 300 символов
  353.     $ma = array();
  354.     for ($aid1 = 0; $aid1 < $count_a; $aid1++) {
  355.         $strlen_a = strlen(strip_tags($i_a[1][$aid1]));
  356.         $ma[] = $strlen_a;
  357.         $m_sum = array_sum($ma);
  358.     }
  359.     $Count_ma = count($ma);
  360.     if($Count_ma >0){
  361.         $max = max($ma); // выбираем самый большой элемент из массива ma
  362.         if ($max < 300){$text=""; /* exit("Самый большой текст между ссылками < 300"); */ }
  363.     }
  364.     echo "Самый большой кусрк между ссылок: $max<br>";
  365.     echo "Длина между ссылок: $m_sum<br>";
  366.  
  367. // Обнуляем если длина ссылок больше длины между ссылок
  368.     $match_a = '/<a>(.+?)<\/a>/ism';
  369.     $run_title = preg_match_all($match_a, $text, $i_a);
  370.     $count_a = count($i_a[0]); // считаем кол-во элементов массива num
  371.    
  372.     $sa = array();
  373.     for ($aid1 = 0; $aid1 < $count_a; $aid1++) {
  374.         $strlen_a = strlen(strip_tags($i_a[1][$aid1]));
  375.         $sa[] = $strlen_a;
  376.         $link_sum = array_sum($sa);
  377.     }
  378.     echo "Длина внутри ссылок: $link_sum<br>";
  379.     if ($link_sum > $m_sum){$text=""; /* exit("Длина ссылок больше длины между ссылок"); */}
  380.  
  381. # ЕСЛИ НЕСКОЛЬКО СЛОВ КОММЕНТАРИЕВ ТО ЭТО ОГЛАВЛЕНИЕ ###################
  382.    /*
  383.     $match_num = '/омментариев/is';
  384.     $run_title = preg_match_all($match_num, $text, $i_comm);
  385.     $count_comm = count($i_comm[0]);
  386.     echo "count_comm = $count_comm<br>";
  387.     if ($count_comm > 4){$text=""; exit("На этой странице больше 4 слов омментариев");}
  388.     */
  389.  
  390. ################### РАЗДЕЛЯЕМ НА <Р> ССЫЛКИ ###################
  391.  
  392. $match_num = '/<p>(.+?)<\/p>/ism';
  393. $run_title = preg_match_all($match_num, $text, $i_num);
  394. $count_num = count($i_num[0]);
  395. //var_dump($i_num);
  396. echo "Всего абзацев Р обнаружено: $count_num<br>";
  397. $only_echo = array();
  398. ################### ОБРАБОТКА <Р> В ПЕРВОМ МАССИВЕ ###################
  399.  
  400. for ($id1 = 0; $id1 < $count_num; $id1++) {
  401.    
  402.     #ZZZZZZZZZZZZZZZZZZZZZZZZ удаляю все после комментариев ZZZZZZZZZZZZZZZZZZZZZZZZ
  403.    $count_s_word = count($stop_content_word);
  404.     for ($iw = 0; $iw < $count_s_word; $iw++) {
  405.         $word=$stop_content_word[$iw];
  406.         $pos = strpos($i_num[0][$id1], $word);
  407.         if ($pos === false){/* ничего не делаем */}
  408.         else {
  409.                 for ($idp = $id1-1; $idp < $count_num; $idp++) {
  410.                     unset($i_num[0][$idp]);
  411.                 }
  412.              }
  413.     }
  414.     #ZZZZZZZZZZZZZZZZZZZZZZZZ обнуляю P со стоп-словами ZZZZZZZZZZZZZZZZZZZZZZZZ
  415.    $count_c_word = count($clean_content_word);
  416.     for ($iw = 0; $iw < $count_c_word; $iw++) {
  417.         $word=$clean_content_word[$iw];
  418.         $pos = strpos($i_num[0][$id1], $word);
  419.         if ($pos === false){// ничего не делаем
  420.             }
  421.         else{unset($i_num[0][$id1]);}
  422.     }
  423.     #ZZZZZZZZZZZZZZZZZZZZZZZZ обнуляю P с URL ZZZZZZZZZZZZZZZZZZZZZZZZ
  424.    if(preg_match('/[a-zA-Z]\.[a-zA-Z]/', $i_num[0][$id1], $matches) ){
  425.     if ($matches) {unset($i_num[0][$id1]);}
  426.     }
  427.     #ZZZZZZZZZZZZZZZZZZZZZZZZ обнуляю P с датой ZZZZZZZZZZZZZZZZZZZZZZZZ
  428.    if(preg_match('/([0-2]\d|3[01])\.(0\d|1[012])\.(\d{4})/i', $i_num[0][$id1], $matches) ){
  429.     if ($matches) {unset($i_num[0][$id1]);}
  430.     }
  431.     #ZZZZZZZZZZZZZZZZZZZZZZZZ Убираю все <b> если нет закрывающей </b> ZZZZZZZZZZZZZZZZZZZZZZZZ
  432.    $open_b = 0;
  433.     $close_b = 0;
  434.     if(preg_match('/<b>/i', $i_num[0][$id1], $matches) ){
  435.     if ($matches) {$open_b = 1; /* echo "Да ТУТ ЕСТЬ ОТКР. Б <br> open_b = $open_b<br>"; */}
  436.     }
  437.     if(preg_match('/<\/b>/i', $i_num[0][$id1], $matches) ){
  438.     if ($matches) {$close_b = 1; /* echo "Да ТУТ ЕСТЬ ЗАКР. Б <br> close_b = $close_b<br>"; */}
  439.     }
  440.     if($open_b == 1 && $close_b == 0){
  441.         $i_num[0][$id1] = preg_replace('/<b>/i', '', $i_num[0][$id1]);
  442.     }
  443.     #ZZZZZZZZZZZZZZZZZZZZZZZZ если в Р нет ни одной русской буквы удаляю ZZZZZZZZZZZZZZZZZZZZZZZZ
  444.    if(preg_match('/[а-я]/', $i_num[0][$id1], $matches4) ){
  445.     if ($matches4) {}
  446.     }else {unset($i_num[0][$id1]);}
  447.     #ZZZZZZZZZZZZZZZZZ если есть заголвок пока ничего не делаю, если нет то стираю < 200 ZZZZZZZZZZZZZZZZ
  448.    $long_id = strlen(strip_tags($i_num[0][$id1]));
  449.     if(preg_match('/<h[1-6]>/',$i_num[0][$id1],$matches) ){
  450.     if ($matches) {}
  451.     }
  452.     else {
  453.         if($long_id < 200){unset($i_num[0][$id1]);}
  454.          }
  455.  
  456.     #ZZZZZZZZZZZZZZZZZZZZZZZZ если элемент всё ещё существует - вывожу ZZZZZZZZZZZZZZZZZZZZZZZZ
  457.    if(isset($i_num[0][$id1]) && strlen(strip_tags($i_num[0][$id1])) > 0)
  458.         {
  459.             // $i_num[0][$id1] = preg_replace("/<\/h1>/is", "</h1>\n", $i_num[0][$id1]);
  460.             // $i_num[0][$id1] = preg_replace("/<\/p>/is", "</p>\n", $i_num[0][$id1]);
  461.             $i_num[0][$id1] = preg_replace('/<p>[0-9]\.\s/i', '<p>', $i_num[0][$id1]);
  462.            
  463.         // echo strlen(strip_tags($i_num[0][$id1]));
  464.         // echo "{$i_num[0][$id1]}";
  465.         $n_mass++;
  466.         $only_echo[] = $i_num[0][$id1];
  467.         }
  468.  
  469. }
  470.  
  471. // echo "<br><br><br>ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ<br><br><br>";
  472. $oe_num = count($only_echo);
  473. // Отладка
  474. echo "oe_num = $oe_num<br>";
  475. // var_dump($only_echo);
  476.  
  477. ################### ОБРАБОТКА <Р> ВО ВТОРОМ МАССИВЕ ###################
  478.  
  479.  
  480.  
  481. if($n_mass > 4){
  482.    
  483.     #TTTTTTTTTTTTTTTTTTT сначала убираем <p> вокруг заголовков TTTTTTTTTTTTTTTTTTT
  484.    for ($i = 0; $i < $oe_num; $i++) {
  485.    
  486.     // $i_num[0][$i] = preg_replace("/\n<\/p>/is", "</p>", $only_echo[$i]);
  487.    
  488.         if(preg_match('/<h[1-6]>/is',$only_echo[$i],$matches) ){
  489.             // var_dump($matches);
  490.             if ($matches) {
  491.                 echo "";
  492.                     $only_echo[$i] = preg_replace("/<p>/is", "", $only_echo[$i]);
  493.                     $only_echo[$i] = preg_replace("/<\/p>/is", "", $only_echo[$i]);
  494.                     $only_echo[$i] = preg_replace("/<h2>/is", "<h1>", $only_echo[$i]);
  495.                     $only_echo[$i] = preg_replace("/<h3>/is", "<h1>", $only_echo[$i]);
  496.                     $only_echo[$i] = preg_replace("/<h4>/is", "<h1>", $only_echo[$i]);
  497.                     $only_echo[$i] = preg_replace("/<h5>/is", "<h1>", $only_echo[$i]);
  498.                     $only_echo[$i] = preg_replace("/<h6>/is", "<h1>", $only_echo[$i]);
  499.                     $only_echo[$i] = preg_replace("/<\/h2>/is", "</h1>", $only_echo[$i]);
  500.                     $only_echo[$i] = preg_replace("/<\/h3>/is", "</h1>", $only_echo[$i]);
  501.                     $only_echo[$i] = preg_replace("/<\/h4>/is", "</h1>", $only_echo[$i]);
  502.                     $only_echo[$i] = preg_replace("/<\/h5>/is", "</h1>", $only_echo[$i]);
  503.                     $only_echo[$i] = preg_replace("/<\/h6>/is", "</h1>", $only_echo[$i]);
  504.                     $only_echo[$i] = preg_replace("/<h1>/is", "<z1><h1>", $only_echo[$i]);
  505.             }
  506.         }
  507.         else {echo "";}
  508.     // echo "{$i_num[0][$i]}";
  509.     }
  510.    
  511. // var_dump($only_echo);
  512.  
  513.     #TTTTTTTTTTTTTTTTTTT уберём предыдущий заголовок если в этом тоже есть TTTTTTTTTTTTTTTTTTT
  514.    for ($i2 = 1; $i2 < $oe_num; $i2++) {
  515.        
  516.         $i_nex = $i2+1;
  517.         $i_pred = $i2-1;
  518.         if($i_pred <= 0){$i_pred = 0;}
  519.        
  520.         //echo "{$i_num[0][$i2]}";
  521.        
  522.         if(preg_match('/<h[1-6]>/is',$only_echo[$i2],$matches) ){
  523.             // var_dump($matches);
  524.            
  525.             if ($matches)
  526.             {
  527.                 //echo "Тут есть H1-6 ";
  528.    
  529.                     if(preg_match('/<h[1-6]>/is',$only_echo[$i_pred],$matches2) ){
  530.                         // var_dump($matches);
  531.                         if ($matches2) {
  532.                             unset($only_echo[$i_pred]);
  533.                         }
  534.                     }
  535.                     else { echo "";}
  536.    
  537.                     if(!isset($only_echo[$i_nex]) ){
  538.                         //echo "Сработал isset";
  539.                         unset($only_echo[$i2]);
  540.                     }
  541.                     else { echo "";}
  542.             }
  543.         }
  544.         else {}
  545.     }
  546.  
  547.     #TTTTTTTTTTTTTTTTTTT вот это я не помню что мы делаем TTTTTTTTTTTTTTTTTTT
  548.    $only_echo[] = "<z1>";
  549.     $content = "";
  550.    
  551.     for ($i3 = 0; $i3 < $count_num; $i3++) {
  552.         $p = $count_num - 1;
  553.        
  554.         if(isset($only_echo[$i3])) {
  555.             // echo "{$only_echo[$i3]}";
  556.             $content .= "{$only_echo[$i3]}";
  557.              // if($i3 == $p) { $content .= "<z1>"; echo "ДА ПРИКРЕПИЛ Z1";}
  558.             }
  559.     }
  560.    
  561. //var_dump($only_echo);
  562.  
  563.  
  564.     #TTTTTTTTTTTTTTTTTTT объединяем блоки в элементы массива по Z1 TTTTTTTTTTTTTTTTTTT
  565.    $content .= "<z1>";
  566.    
  567.     // echo "<br><br><br><br><br><br><br><br><br><br><br>$content<br><br><br><br><br><br><br><br><br><br><br>";
  568.    
  569.     $match_z1 = '/<z1>(.+?)<z1>/ism';
  570.     $run_title = preg_match_all($match_z1, $content, $final);
  571.     $count_num = count($final[0]); // считаем кол-во элементов массива num
  572.    
  573. // var_dump($final[1]); // - это итоговый var_dump, включать смотреть только его
  574.  
  575. $f_bd = count($final[1]);
  576.  
  577.     #BBBBBBBBBBBBBBBBBBB Пишем элементы массива в БД BBBBBBBBBBBBBBBBBBBBBBB
  578.    for ($i4 = 0; $i4 < $f_bd; $i4++) {
  579.         if(isset($final[1][$i4])) {
  580.             $mytext = $final[1][$i4];
  581.             // echo "ЭТО ТЕКСТ В БАЗУ ДАННЫХ:<br> $mytext";
  582.             mysqli_query($link, "INSERT INTO `".$time_table1."` SET `text`='".$mytext."' ");
  583.             }
  584.     }
  585.     $only_echo = array();
  586.     $content = "";
  587.  
  588. // тут будет выемка из БД и запись в файл
  589.  
  590. } // конец $n_mass > 4
  591.  
  592. ############################# ЗАВЕРШАБЩИЙ БЛОК #########################
  593.  
  594.  
  595.             } else {
  596.                 echo "curl_init failure! <br/>\r\n";
  597.             }
  598.             curl_close($main);
  599.         }
  600.        
  601.     } // конец foreach
  602.    
  603. ############################# РАБОТАЕМ ПОСЛЕ foreach ТОЛЬКО С $maintext
  604. }
  605.  
  606.  
  607.  
  608.  
  609. #VVVVVVVVVVVVVVVVVVVVVVVVVV Вынимаю данные из базы для создания статьи VVVVVVVVVVVVVVVVVVVVVVVVVV
  610.    $res = mysqli_query($link," SELECT `text` FROM `".$time_table1."` ");
  611.     if($res) { //если запрос успешный
  612.         while($row = mysqli_fetch_assoc($res)) {
  613.             $mytxt[] = $row;
  614.         }  
  615.             $m_long = count($mytxt);
  616.             $gottext = "";
  617.             //echo "<br><br><br><br><br><br> А это вывод из базы mytxt <br><br><br><br><br><br><br>";
  618.             var_dump($mytxt);
  619.            
  620.             $narast_long = 0;
  621.             for ($i5 = 0; $i5 < $m_long; $i5++) {
  622.                 $gottext .= $mytxt[$i5]['text'];
  623.                 $longelement = strlen($mytxt[$i5]['text']);
  624.                 $narast_long = $narast_long + $longelement;
  625.                 echo "longelement = $longelement<br>";
  626.                 echo "narast_long = $narast_long<br>";
  627.                 if($narast_long > 20000){ break; } // ЭТО МАКСИМАЛЬНЫЙ РАЗМЕР СТАТЬИ !!
  628.                
  629.             }
  630.             //echo "<br><br><br><br><br><br> А это вывод gottext <br><br><br><br><br><br><br> $gottext";
  631.            
  632.         }
  633.        
  634. #VVVVVVVVVVVVVVVVVVVVVVVVVV Пишем элементы массива в БД VVVVVVVVVVVVVVVVVVVVVVVVVV
  635. $fname = microtime(true); //имя файла нового
  636. $file="html/$fname.html";
  637.  
  638. // вставка с дизайном
  639. include 'grabber_site_xml_list_design.php';
  640.  
  641. mysqli_query($link, " TRUNCATE `".$time_table1."` ");
  642.  
  643. $g2 = microtime(true);
  644. $g3 = $g2 - $g1; // время генерации
  645. echo "\n<br>Время регулярок: ". $g3 ."<br>\n";
  646.  
  647. ?>
  648.  
  649. <form action="" method="POST">
  650.     <textarea name="text" cols="50">http://updiet.info/kak-poxudet-v-domashnix-usloviyax.html</textarea>
  651.     <button type="submit" name="add">Add</button>
  652. </form>
  653.  
  654. </body>
  655. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement