На PHP собрать все файлы *.json в папках и подпапках Может быть очень много файлов
Пример пути attachments\2020\06\04\b12783481fafe55fbca93d66d9db9d32\1591269626.json
Каждый файл *.json (пример 1591269626.json) имеет массив
Пример структуры массива
{"status":"success","data":{"0":{"uid":6,"subject":"\u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043a\u043e\u043d\u0441\u0443\u043b\u044c\u0442\u0430\u0446\u0438\u044f","date":"2020-06-04 02:20:24pm","from":{"address":"Kvokov@sar-steklo.ru","name":"\u041a\u0432\u043e\u043a\u043e\u0432 \u0421\u0435\u0440\u0433\u0435\u0439 \u041f\u0435\u0442\u0440\u043e\u0432\u0438\u0447"},"message":"<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http:\/\/schemas.microsoft.com\/office\/2004\/12\/omml\" xmlns=\"http:\/\/www.w3.org\/TR\/REC-html40\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=koi8-r\">\r\n<meta name=\"Generator\" content=\"Microsoft Word 14 (filtered medium)\">\r\n<style><!--\r\n\/* Font Definitions *\/\r\n@font-face\r\n\t{font-family:Calibri;\r\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\r\n\/* Style Definitions *\/\r\np.MsoNormal, li.MsoNormal, div.MsoNormal\r\n\t{margin:0cm;\r\n\tmargin-bottom:.0001pt;\r\n\tfont-size:11.0pt;\r\n\tfont-family:\"Calibri\",\"sans-serif\";\r\n\tmso-fareast-language:EN-US;}\r\na:link, span.MsoHyperlink\r\n\t{mso-style-priority:99;\r\n\tcolor:blue;\r\n\ttext-decoration:underline;}\r\na:visited, span.MsoHyperlinkFollowed\r\n\t{mso-style-priority:99;\r\n\tcolor:purple;\r\n\ttext-decoration:underline;}\r\nspan.EmailStyle17\r\n\t{mso-style-type:personal-compose;\r\n\tfont-family:\"Calibri\",\"sans-serif\";\r\n\tcolor:windowtext;}\r\n.MsoChpDefault\r\n\t{mso-style-type:export-only;\r\n\tfont-family:\"Calibri\",\"sans-serif\";\r\n\tmso-fareast-language:EN-US;}\r\n@page WordSection1\r\n\t{size:612.0pt 792.0pt;\r\n\tmargin:2.0cm 42.5pt 2.0cm 3.0cm;}\r\ndiv.WordSection1\r\n\t{page:WordSection1;}\r\n--><\/style><!--[if gte mso 9]><xml>\r\n<o:shapedefaults v:ext=\"edit\" spidmax=\"1026\" \/>\r\n<\/xml><![endif]--><!--[if gte mso 9]><xml>\r\n<o:shapelayout v:ext=\"edit\">\r\n<o:idmap v:ext=\"edit\" data=\"1\" \/>\r\n<\/o:shapelayout><\/xml><![endif]-->\r\n<\/head>\r\n<body lang=\"RU\" link=\"blue\" vlink=\"purple\">\r\n<div class=\"WordSection1\">\r\n<p class=\"MsoNormal\">?????? ????.<o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\">????? ? ??? ? ???? ????? ??????????<o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\">???????.<o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\"><o:p> <\/o:p><\/p>\r\n<p class=\"MsoNormal\">?????? ?????? ????????,<o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\">????????? ??????? ???????????? ??????? ???????? ? ?????????<o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\">?? «??????????????????» <o:p><\/o:p><\/p>\r\n<p class=\"MsoNormal\">???. 8 8452 305232 8 987 811 30 34<o:p><\/o:p><\/p>\r\n<\/div>\r\n<\/body>\r\n<\/html>\r\n","attachments":[]},"udate":1591269626,"uid_id":"<E65DD1F8E9B14947BA267F8534FA2F5BF1E65DE9@Igor16.steklo.com>","inbox_id":404,"message_id":null}}
В этом массиве осуществить поиск в "subject" "address" "message" по ключевому слову который приходит с запросом POST globSearch
Собранные совпадение собрать в Массив $globOutput
I'll create a PHP script that recursively searches for *.json files in the specified directory structure, processes each file, and searches for a keyword from a POST request in the specified fields, collecting matches in an array.
search_json.php
php
This script:
- Initializes an empty $globOutput array to store results
- Gets the search keyword from $_POST['globSearch']
- Uses RecursiveIteratorIterator to efficiently traverse directories and find all *.json files
- Processes each JSON file, checking for the keyword in subject, from.address, and message fields
- Uses case-insensitive search (stripos)
- Collects matches with relevant details (file path, record index, matched fields, and record metadata)
- Handles errors for invalid JSON, inaccessible directories, or other issues
- Limits message content to 200 characters to prevent excessive output
- Returns results as JSON with Unicode support
To use this script:
- Save it as search_json.php
- Send a POST request with globSearch parameter containing the search keyword
- The script will return a JSON array of matches with details about where the keyword was found
The output format will look like:
json
[
{
"file": "attachments/2020/06/04/b12783481fafe55fbca93d66d9db9d32/1591269626.json",
"record_index": 0,
"matches": {
"subject": "техническая консультация",
"address": "Kvokov@sar-steklo.ru"
},
"record": {
"uid": 6,
"subject": "техническая консультация",
"date": "2020-06-04 02:20:24pm",
"from": {
"address": "Kvokov@sar-steklo.ru",
"name": "Квоков Сергей Петрович"
},
"udate": 1591269626,
"uid_id": "<E65DD1F8E9B14947BA267F8534FA2F5BF1E65DE9@Igor16.steklo.com>",
"inbox_id": 404
}
}
]The script is optimized for handling large numbers of files and includes error handling for robustness. Make sure the PHP process has enough memory and execution time for very large directories (you might need to adjust memory_limit and max_execution_time in php.ini for very large datasets).
1,7s