Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 KB | None | 0 0
  1. <html>
  2.   <head>
  3.       <meta charset="utf-8">
  4.     <title>Парсер</title>
  5.   </head>
  6.   <body>
  7.       <form action="" method=post enctype=multipart/form-data>
  8.   <label for="userfile"> Выберите файл:</label>
  9. <input type="file" name="userfile"/>
  10. <input type='submit' value="Загрузить"/></form>
  11. <?php
  12.                                
  13.  
  14.     $upfile = 'uploads/'.$_FILES['userfile']['name'];
  15.    
  16. if (is_uploaded_file($_FILES['userfile']['tmp_name']))
  17. {
  18.     if(! move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
  19.     {
  20.         echo 'Невозможно переместить файл в каталог назначения';
  21.         exit();
  22.     }
  23. }
  24. else
  25. {
  26.        echo 'Выберите файл в формате txt';
  27.         exit();
  28. }
  29. echo "Файл успешно загружен";
  30. echo "Имя файла:".$_FILES['userfile']['name']."<p><a href='files/fileEXEL.csv' download>Скачать файл для EXEL</a></p><p><a href='files/file.csv' download>Скачать файл c кодировкой UTF8</a></p>";
  31.  
  32. $a = file_get_contents($upfile);
  33.  
  34. // Вывод содержимого
  35.  
  36. $dom = new DOMDocument;
  37. @$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $a);
  38.  
  39. $way = $dom->getElementsByTagName('way');
  40. $arr = [];
  41. foreach ($way as $v) {
  42.     $tag = $v->getElementsByTagName('tag');
  43.     $user = $v->getAttribute('user');
  44.     $arr[$user][] = [];
  45.     $id = $v->getAttribute('timestamp');
  46.     $nd = $v->getElementsByTagName('nd');
  47.     foreach ($tag as $v) {
  48.         $arr[$user][count($arr[$user]) - 1][] = [];
  49.         foreach ($v->attributes as $k => $v) {
  50.             $arr[$user][count($arr[$user]) - 1][count($arr[$user][count($arr[$user]) - 1]) - 1][$k] = $v->textContent;
  51.                  
  52.         }
  53.     }
  54.  
  55.     $arr[$user][count($arr[$user]) - 1]['timestamp'] = $id;
  56.  
  57.     $ref=[];
  58.     foreach ($nd as $v) {
  59.         $ref[]=$v->getAttribute('ref');
  60.    
  61.     }
  62.    
  63.    $arr[$user][count($arr[$user]) -1]['ref'] = $ref;
  64.     //echo  $ref;
  65.    
  66.     //$ref=$ref[0]."-".$ref[1]."-".$ref[2]."-".$ref[3]."-".$ref[4]."-".$ref[5]."-".$ref[6]."-".$ref[7]."-".$ref[8];
  67. //print ($ref[2]);
  68.  
  69. }
  70.  
  71. $buf = '';
  72. $dom = new DOMDocument;
  73. @$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $a);
  74. $node = $dom->getElementsByTagName('node');
  75. $arr2 = [];
  76. foreach ($node as $k0 => $v) {
  77.  
  78.     $arr2[$k0] = [];
  79.     foreach ($v->attributes as $k1 => $v) {
  80.         $arr2[$k0][$k1] = $v->textContent;
  81.        
  82.     }
  83. }
  84.  
  85. function get($arr, $ref)
  86. {
  87.     print ($ref);
  88.     $buf = "(<span style='color: red;'>Данные не найдены по id=$ref</span>)";
  89.     foreach ($arr as $v) {
  90.    
  91.         if (isset($v['id'], $v['lat'], $v['lon'])) {
  92.             if ($v['id'] == $ref) {
  93.                 $buf = $v['lat'] . ' - ' . $v['lon'];
  94.        
  95.  
  96.    
  97.             }
  98.         }
  99.     }
  100.     return $buf;
  101. }
  102.      
  103.  
  104.       echo '<table cellspacing="0" border="1">
  105.   <tr>
  106.    <th width="150px">Район</th>
  107.     <th width="80px">Номер дома</th>
  108.     <th width="150px">Улица</th>
  109.     <th width="200px">Координаты</th>
  110.     <th width="200px">Дата</th>
  111.   </tr>';
  112.  
  113.      
  114. $csv="";
  115.      
  116. foreach ($arr as $v) {
  117.  
  118.    
  119.     foreach ($v as $v) {
  120.        
  121.         if (isset($v[0]['k'], $v[0]['v'], $v[1]['k'], $v[1]['v'], $v[2]['k'], $v[2]['v'], $v[3]['k'], $v[3]['v'], $v['ref'], $v['timestamp'])&&isset($v['ref'][0] , $v['ref'][1])) {
  122.           echo  "<tr><td align='center'>". $v[0]['v'] . "</td><td align='center'>" . $v[1]['v'] . "</td><td align='center'>" . $v[2]['v'] . "</td><td align='center'>". get($arr2, $v['ref'][0], $v['ref'][1]) ."</td><td align='center'>". $v['timestamp']."</td></tr>"."\n";
  123.             $file= $v[0]['v'] .";". $v[1]['v'] .";". $v[2]['v'] . "; ". get($arr2, $v['ref'][0]) .";". "\n";
  124.             $str = iconv('UTF-8', 'CP1251', $file);
  125.         $csv.= $str;
  126.  
  127.         }
  128.     }
  129. }
  130.  
  131. file_put_contents("files/fileEXEL.csv", $csv);
  132.  
  133.       $csv2="";
  134.      
  135. foreach ($arr as $v) {
  136.    
  137.    
  138.     foreach ($v as $v) {
  139.        
  140.         if (isset($v[0]['k'], $v[0]['v'], $v[1]['k'], $v[1]['v'], $v[2]['k'], $v[2]['v'], $v[3]['k'], $v[3]['v'], $v['ref'])&&isset($v['ref'][0])) {
  141.        
  142.             $file2= $v[0]['v'] .";". $v[1]['v'] .";". $v[2]['v'] . "; ". get($arr2, $v['ref'][0]) .";". "\n";
  143.  
  144.         $csv2.= $file2;
  145.  
  146.         }
  147.     }
  148. }
  149.  
  150. file_put_contents("files/file.csv", $csv2);
  151.                                    
  152. ?>
  153.  
  154.  
  155.      
  156.  
  157.   </body>
  158. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement