-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.php
More file actions
125 lines (100 loc) · 5.64 KB
/
Copy pathform.php
File metadata and controls
125 lines (100 loc) · 5.64 KB
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
<div align=center>
<form method="post" enctype="multipart/form-data">
<input type="file" name="docs[]" multiple> <br>
<textarea name="description"></textarea> <br>
<input type="submit">
</form>
</div>
<?php
$fromfdir = __DIR__ . DIRECTORY_SEPARATOR . 'fromfile'; // директория куда будут помещены файлы сформированные из файла
$fromtextdir = __DIR__ . DIRECTORY_SEPARATOR . 'fromtextarea';// директория куда будут помещены файлы сформированные из текстового поля
$startnamef = 'fromf.txt'; // базовое имя для файлов сформированных из файла
$startnamet = 'fromt.txt'; // базовое имя для файлов сформированных из текстового поля
if ($_POST['description'] == '' and $_FILES['docs']['name']['0'] == '') {
echo 'Введите текст или загрузите файл с текстом';
} else {
if ($_POST['description'] !== '' and $_FILES['docs']['name']['0'] !== '') { // обрабатываем все поля
$array_from_tarea = count_word_from_text($_POST['description']);
array_to_csv($array_from_tarea, save_file($startnamet, $fromtextdir));
if ($_FILES['docs']['type']['0'] !== 'text/plain') {
echo 'Загружаемый файл не является тестовым !';
} else {
$text_from_file = get_text_from_file($_FILES['docs']);
$array_from_file = count_word_from_text($text_from_file);
array_to_csv($array_from_file, save_file($startnamef, $fromfdir));
}
} else {
if ($_POST['description'] !== '' and $_FILES['docs']['name']['0'] == '') { //обрабатываем текстовое поле
$array_from_tarea = count_word_from_text($_POST['description']);
array_to_csv($array_from_tarea, save_file($startnamet, $fromtextdir));
}
if ($_POST['description'] == '' and $_FILES['docs']['name']['0'] !== '') { //обрабатываем файловое поле
if ($_FILES['docs']['type']['0'] !== 'text/plain') {
echo 'Загружаемый файл не является тестовым !';
} else {
$text_from_file = get_text_from_file($_FILES['docs']);
$array_from_file = count_word_from_text($text_from_file);
array_to_csv($array_from_file, save_file($startnamef, $fromfdir));
}
}
}
}
function array_to_csv($array, $outfile)// записывает ассоциативный массив (слово => количество слов) в файл
{
foreach ($array as $word => $count) {
fwrite(fopen($outfile, 'a'), "{$word};{$count}" . PHP_EOL);
}
}
function save_file($filename, $dir) // функция сохраняет файл с префиксом, который позволяет не затирать файл
{
if (is_dir($dir)) {
} else {
mkdir($dir);
}
$info = pathinfo($filename);
$name = $dir . '/' . $info['filename'];
$prefix = '';
$ext = (empty($info['extension'])) ? 'csv' : '.' . 'csv';
if (is_file($name . $ext)) {
$i = 1;
$prefix = '_' . $i;
while (is_file($name . $prefix . $ext)) {
$prefix = '_' . ++$i;
}
}
return $name . $prefix . $ext;
}
function get_text_from_file($docs)// функция возвращает текст взятый из загруженного текстового файла
{
foreach ($docs['tmp_name'] as $index => $tmpPath) {
if (!array_key_exists($index, $docs['name'])) {
continue;
}
move_uploaded_file($tmpPath, __DIR__ . DIRECTORY_SEPARATOR . $docs['name'][$index]);
$contents = file_get_contents($docs['name'][$index]);
unlink($docs['name'][$index]);
}
return $contents;
}
function count_word_from_text($text_to_count)// функция создает ассоциированный массив где ключ - слово, а значение - количество слов
{
if ($text_to_count == '') {
exit;
} else {
$char_to_delete = [".", "-", "...", "!", "*", "?", ',', ",", "\n", "\n\r", '\n\r', "\n", '\n']; // задаем массив непечатаемых символов и прочих для их удаления
$spaces = [' ', ' ', ' ', ' ', ' ']; // задаем массив строк с различными вариантами пробелов для их отлова и удаления
$text_without_char = str_replace($char_to_delete, '', $text_to_count); // чистим текст от лишних символов, можно конечно исползовать REGEXP
$text_without_spaces = str_replace($spaces, ' ', $text_without_char); // чистим текст от 2-х и более пробелов
$text_to_array = explode(' ', trim(mb_strtolower($text_without_spaces))); // добавляем текст в массив так, чтобы кажде слово было отдельным его элементом
$array_assoc = []; // объявляем пустой массив
foreach ($text_to_array as $word) {
if (array_key_exists($word, $array_assoc)) {
$array_assoc[$word]++;
} else {
$array_assoc[$word] = 1;
}
}
return $array_assoc;
}
}
?>