/
script.php
executable file
·133 lines (119 loc) · 3.3 KB
/
script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
header('Content-Type: text/html; charset=utf-8');
include 'utils.php';
doit();
/**
* Init converting. Execute read and write to files
* @return nothing
*/
function doit() {
$input = 'Input/';
$output = 'Output/';
$path = array(
'1/01.LAS',
'1/02.LAS',
'20/03.LAS',
'20/04.LAS'
);
foreach($path as $f) {
$file = file($input.$f);
convert($file);
if(file_exists($output.$f))
file_put_contents($output.$f, $file);
else {
$dir = substr($f, 0, strpos($f, '/'));
mkdir($output.$dir, 0777, true);
file_put_contents($output.$f, $file);
}
}
}
/**
* convert array with needle instructions
* @param array &$arr
* @return nothing
*/
function convert(&$arr) {
foreach ($arr as $key => $value) {
$arr[$key] = iconv('CP866', 'utf-8', $value);
}
deleteOnceFromArray($arr, 'comment');
deleteOnceFromArray($arr, 'MNEM', 'MEST');
translit($arr, array('FLD ', 'COMP', 'NAME'));
changeDescText($arr, 'NAME', 'Компания оцифровщик каротажа');
foreach ($arr as $key => $value) {
$arr[$key] = iconv('utf-8', 'CP866', $value);
}
}
/**
* Delete from LAS file Data with different type, such as 'MNEM, UNIT, DATA, DESC'
* @param array &$arr
* @param string $type
* @param string $data
* @return bool
*/
function deleteOnceFromArray(&$arr, $type, $data) {
foreach($arr as $key => $value) {
if($type == 'comment') {
if($value[0] == '~' && $value[1] == 'W') break;
else if($value[0] == '#') {
unset($arr[$key]);
}
}
else if ($data == getLASElement($type, $value)) {
unset($arr[$key]);
break;
}
}
return true;
}
/**
* Change text in type 'DESC'
* @param array &$arr
* @param string $field
* @param string $data
* @return nothing
*/
function changeDescText(&$arr, $field, $data) {
foreach($arr as $key => $value) {
if ($field == getLASElement('MNEM', $value)) {
$arr[$key] = getLASElement('MNEM', $value).'.'.getLASElement('UNIT', $value).' '.getLASElement('DATA', $value).':"'.$data."\"\n";
break;
}
}
}
/**
* Init translit and set result in array
* @param array &$arr
* @param string $fields
* @return nothing
*/
function translit(&$arr, $fields) {
$i = 0;
foreach($arr as $key => $value) {
if($i == count($fields)) break;
if(in_array(getLASElement('MNEM', $value), $fields)) {
$i++;
$data = getLASElement('DATA', $value);
$arr[$key] = getLASElement('MNEM', $value).'.'.getLASElement('UNIT', $value).' '.parseToTranslit($data).':'.getLASElement('DESC', $value);//."\n";
}
}
}
/**
* Parsing cyrillic by translit table
* @param string $stroke
* @return string
*/
function parseToTranslit($stroke) {
$translitArr = getTranslitTable();
$str = '';
for($i = 0; $i < strlen($stroke); $i++) {
$char = mb_substr($stroke, $i, 1, 'UTF-8');
$tmp = preg_match('~^[А-ЯЁ\W]~u', $char) ? true : false;
$char = mb_strtoupper($char, 'UTF-8');
if ($char != ' ')
$str .= $tmp ? $translitArr[$char] : mb_strtolower($translitArr[$char]);
else $str .= ' ';
}
return $str;
}
?>