Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta charset="utf-8">
- <title>Парсер</title>
- </head>
- <body>
- <form action="" method=post enctype=multipart/form-data>
- <label for="userfile"> Выберите файл:</label>
- <input type="file" name="userfile"/>
- <input type='submit' value="Загрузить"/></form>
- <?php
- $upfile = 'uploads/'.$_FILES['userfile']['name'];
- if (is_uploaded_file($_FILES['userfile']['tmp_name']))
- {
- if(! move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
- {
- echo 'Невозможно переместить файл в каталог назначения';
- exit();
- }
- }
- else
- {
- echo 'Выберите файл в формате txt';
- exit();
- }
- echo "Файл успешно загружен";
- 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>";
- $a = file_get_contents($upfile);
- // Вывод содержимого
- $dom = new DOMDocument;
- @$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $a);
- $way = $dom->getElementsByTagName('way');
- $arr = [];
- foreach ($way as $v) {
- $tag = $v->getElementsByTagName('tag');
- $user = $v->getAttribute('user');
- $arr[$user][] = [];
- $id = $v->getAttribute('timestamp');
- $nd = $v->getElementsByTagName('nd');
- foreach ($tag as $v) {
- $arr[$user][count($arr[$user]) - 1][] = [];
- foreach ($v->attributes as $k => $v) {
- $arr[$user][count($arr[$user]) - 1][count($arr[$user][count($arr[$user]) - 1]) - 1][$k] = $v->textContent;
- }
- }
- $arr[$user][count($arr[$user]) - 1]['timestamp'] = $id;
- $ref=[];
- foreach ($nd as $v) {
- $ref[]=$v->getAttribute('ref');
- }
- $arr[$user][count($arr[$user]) -1]['ref'] = $ref;
- //echo $ref;
- //$ref=$ref[0]."-".$ref[1]."-".$ref[2]."-".$ref[3]."-".$ref[4]."-".$ref[5]."-".$ref[6]."-".$ref[7]."-".$ref[8];
- //print ($ref[2]);
- }
- $buf = '';
- $dom = new DOMDocument;
- @$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . $a);
- $node = $dom->getElementsByTagName('node');
- $arr2 = [];
- foreach ($node as $k0 => $v) {
- $arr2[$k0] = [];
- foreach ($v->attributes as $k1 => $v) {
- $arr2[$k0][$k1] = $v->textContent;
- }
- }
- function get($arr, $ref)
- {
- print ($ref);
- $buf = "(<span style='color: red;'>Данные не найдены по id=$ref</span>)";
- foreach ($arr as $v) {
- if (isset($v['id'], $v['lat'], $v['lon'])) {
- if ($v['id'] == $ref) {
- $buf = $v['lat'] . ' - ' . $v['lon'];
- }
- }
- }
- return $buf;
- }
- echo '<table cellspacing="0" border="1">
- <tr>
- <th width="150px">Район</th>
- <th width="80px">Номер дома</th>
- <th width="150px">Улица</th>
- <th width="200px">Координаты</th>
- <th width="200px">Дата</th>
- </tr>';
- $csv="";
- foreach ($arr as $v) {
- foreach ($v as $v) {
- 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])) {
- 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";
- $file= $v[0]['v'] .";". $v[1]['v'] .";". $v[2]['v'] . "; ". get($arr2, $v['ref'][0]) .";". "\n";
- $str = iconv('UTF-8', 'CP1251', $file);
- $csv.= $str;
- }
- }
- }
- file_put_contents("files/fileEXEL.csv", $csv);
- $csv2="";
- foreach ($arr as $v) {
- foreach ($v as $v) {
- 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])) {
- $file2= $v[0]['v'] .";". $v[1]['v'] .";". $v[2]['v'] . "; ". get($arr2, $v['ref'][0]) .";". "\n";
- $csv2.= $file2;
- }
- }
- }
- file_put_contents("files/file.csv", $csv2);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement