Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $utf_csv = array_map('str_getcsv', file('123.csv'));
- //убираем первый элемент массива - заголовки
- array_shift($utf_csv);
- $file = 'new_all.csv';
- function getPercent($title) {
- $percent = '';
- if (preg_match_all('/\d*[.,]?\d+(\s)?(%)/ui', $title, $matches, PREG_PATTERN_ORDER)) {
- if (mb_substr_count($title, 'скидка') > 0) {
- if (array_key_exists(1, $matches[0])) {
- $percent = array_shift($matches[0]);
- }
- } else {
- $percent = array_pop($matches[0]);
- }
- $percent = str_replace('.', ',', $percent);
- if (preg_match('/(\d*[.,]?\d+)(\s)(%)/ui', $percent)) {
- $percent = preg_replace('/(\d*[.,]?\d+)(\s)(%)/ui','$1' . '$3', $percent);
- }
- }
- return trim($percent);
- }
- function getDosage($title) {
- $dosage = '';
- if (preg_match('/\d+(\s)?(доз([аы])?)\b/ui', $title, $matches)) {
- $dosage = $matches[0];
- $dosage = str_replace(' ', '', $dosage);
- }
- return trim($dosage);
- }
- function getQuantity($title) {
- $quantity = '';
- if (preg_match_all('/\D(№|Х|X|N)(\s)?\d+((\s)?(№|Х|X|N)\d+)?\b/ui', $title, $matches, PREG_PATTERN_ORDER)) {
- $quantity = array_pop($matches[0]);
- if (preg_match('/(Х|X|N)/ui', $quantity)) {
- $quantity = preg_replace('/(Х|X|N)/ui','№', $quantity);
- }
- if (preg_match('/(№)(\s)(\d*[.,]?\d+)/ui', $quantity)) {
- $quantity = preg_replace('/(№)(\s)(\d*[.,]?\d+)/ui','$1' . '$3', $quantity);
- }
- // if (preg_match('/(№)(\d*[.,]?\d+)(№)(\d*[.,]?\d+)/ui', $title)) {
- // $title = preg_replace('/(№)(\d*[.,]?\d+)(№)(\d*[.,]?\d+)/ui','$1' . '$2' . ' ' . '$3' . '$4', $title);
- // }
- }
- return trim($quantity);
- }
- function getUnit($title) {
- $unit = '';
- if (preg_match('/\d*[.,]?\d+(\s|\/)?(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|ле|кв\W+см)\b(\/)?(\d+)?(мг|мл|г|доз([аы])?)?(([\w+\W+]{1,})\d*[.,]?\d+(\s|\/)?(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см|доз([аы])?)(\/)?(\d+)?(мг|мл|г|доз([аы])?)?)?/ui', $title, $matches)) {
- $unit = $matches[0];
- // if (mb_substr_count($title, 'флуимуцил антибиотик') > 0) {
- // echo 1;
- // }
- }
- // if (preg_match('/\s\d*[.,]?\d+(\s)?(\+)(\s)?\d*[.,]?\d+\s/ui', $unit, $matches)) {
- // $unit = $matches[0];
- // }
- //
- // if (preg_match('/(\d*[.,]?\d+)(\s)(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см)/ui', $unit, $matches)) {
- // $unit = preg_replace('/(\d*[.,]?\d+)(\s)(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см)/ui','$1' . '$3', $unit);
- // }
- return $unit;
- }
- function getForm($title) {
- $form = '';
- // if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|спрей|спр|капсулы|капс|крем|гель|мазь|суппозитории|супп|сусп|лак|табл|таб|порошок|пор)(\.)?\b/ui', $title, $matches)) {
- if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|крем|спрей|спр|суппозитории|супп|суспензия|сусп|лак|порошок|пор|аэрозоль|аэр)(\.)?\b/ui', $title, $matches)) {
- $form = $matches[0];
- }
- if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|крем|спрей|спр|суппозитории|супп|суспензия|сусп|лак|порошок|пор|аэрозоль|аэр|свечи|настойка|пастилки|гран|саше)(\.)?\b/ui', $title, $matches)) {
- $form = $matches[0];
- }
- if (preg_match('/(раст|р-р|р-в)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(раст|р-р|р-в)(\.)?/ui','раствор', $form);
- }
- if (preg_match('/(спрей|спр)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(спрей|спр)(\.)?[^а-я]/ui','спрей', $form);
- }
- // if (preg_match('/(капсулы|капс)(\.)?/ui', $form, $matches)) {
- // $form = preg_replace('/(капсулы|капс)(\.)?/ui','капс.', $form);
- // }
- if (preg_match('/(суппозитории|супп)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(суппозитории|супп)(\.)?/ui','суппозитории', $form);
- }
- if (preg_match('/(суспензия|сусп)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(суспензия|сусп)(\.)?/ui','суспензия', $form);
- }
- // if (preg_match('/(таб)(\.)?/ui', $form, $matches)) {
- // $form = preg_replace('/(таб)(\.)?/ui','таб.', $form);
- // }
- if (preg_match('/(порошок|пор)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(порошок|пор)(\.)?/ui', 'порошок', $form);
- }
- if (preg_match('/(аэрозоль|аэр)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(аэрозоль|аэр)(\.)?/ui', 'аэрозоль', $form);
- }
- $form = str_replace('..', '.', $form);
- return trim($form);
- }
- function forChildren($title) {
- $boolean = 0;
- if (preg_match('/\b(дет)(\.)?(\s)?/ui', $title, $matches)) {
- $boolean = 1;
- }
- return $boolean;
- }
- function getPackage($title) {
- $package = '';
- if (preg_match('/\b(амп|туб[аы]?|фл)(\.)?(\s)?/ui', $title, $matches)) {
- $package = $matches[0];
- }
- $package = str_replace('амп', 'амп.', $package);
- $package = str_replace('фл', 'фл.', $package);
- $package = str_replace('..', '.', $package);
- return trim($package);
- }
- $drug = [];
- $drugs = [];
- $results = [];
- foreach ($utf_csv as $utf_row) {
- $true_title = mb_strtolower($utf_row[1]);
- $true_title = str_replace(' ', ' ', $true_title);
- $true_title = str_replace(' ', ' ', $true_title);
- $percent = getPercent($true_title);
- $dosage = getDosage($true_title);
- $package = getPackage($true_title);
- $unit = getUnit($true_title);
- $quantity = getQuantity($true_title);
- $form = getForm($true_title);
- $forChildren = forChildren($true_title);
- $edit_title = $utf_row[2];
- $edit_title = str_replace('для детей', '', $edit_title);
- $edit_title = str_replace(' ', ' ', $edit_title);
- $edit_title = str_replace(' ', ' ', $edit_title);
- if (preg_match('/(раст|р-р|р-в)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(раст|р-р|р-в)(\.)?/ui','', $edit_title);
- }
- if (preg_match('/(спрей|спр)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(спрей|спр)(\.)?[^а-я]/ui','', $edit_title);
- $edit_title = str_replace(' спрей', '', $edit_title);
- }
- if (preg_match('/(суппозитории|супп)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(суппозитории|супп)(\.)?/ui','', $edit_title);
- }
- if (preg_match('/(суспензия|сусп)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(суспензия|сусп)(\.)?/ui','', $edit_title);
- }
- if (preg_match('/(порошок|пор)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(порошок|пор)(\.)?/ui', '', $edit_title);
- }
- if (preg_match('/(аэрозоль|аэр)(\.)?/ui', $edit_title, $matches)) {
- $edit_title = preg_replace('/(аэрозоль|аэр)(\.)?/ui', '', $edit_title);
- }
- if (preg_match('/(саше)(\.)?/ui', $form, $matches)) {
- $form = preg_replace('/(саше)(\.)?/ui', '', $form);
- }
- $edit_title = trim($edit_title);
- $result = [];
- // $drug = [];
- // array_push($drug, $utf_row[0], $true_title, '', $dosage, $utf_row[2], $utf_row[3], $utf_row[4], $utf_row[5], $utf_row[6], $utf_row[7]);
- // array_push($drugs, $drug);
- // if (preg_match('/(амп)/ui', $true_title, $matches)) {
- // if (mb_substr_count($true_title,'р-ра') > 0) {
- // array_push($drug, $true_title, $percent, $dosage, $unit, $quantity, $package, $form, $forChildren);
- // }
- // }
- if ($utf_row[4] == '') {
- array_push($result, $utf_row[0], $utf_row[1], $edit_title, $utf_row[3], $form, $utf_row[5], $utf_row[6], $utf_row[7], $utf_row[8],
- $utf_row[9], $utf_row[10], $utf_row[11], $utf_row[12], $utf_row[13], $utf_row[14], $utf_row[15], $utf_row[16], $utf_row[17]);
- } else {
- array_push($result, $utf_row[0], $utf_row[1], $edit_title, $utf_row[3], $utf_row[4], $utf_row[5], $utf_row[6], $utf_row[7], $utf_row[8],
- $utf_row[9], $utf_row[10], $utf_row[11], $utf_row[12], $utf_row[13], $utf_row[14], $utf_row[15], $utf_row[16], $utf_row[17]);
- }
- array_push($results, $result);
- }
- unset($a);
- unset($b);
- //unlink($file);
- $blas = [];
- $bla = [];
- array_push($bla, 'NAME_ID', 'Full name', 'Name', 'Для детей', 'Форма выпуска', 'Граммовка', 'Количество', 'Способ применения', 'Проценты', 'Доза', 'Вкус', 'Упаковка', 'Остаток', 'ШК', 'Страна', 'Производитель', 'Цена факт', 'Для сайта');
- array_push($blas, $bla);
- foreach ($blas as $result) {
- foreach ($result as $key => $value) {
- $count = count($result);
- if ($key != $count - 1) {
- file_put_contents($file, $value . ";", FILE_APPEND);
- } else {
- file_put_contents($file, $value . ";\n", FILE_APPEND);
- }
- }
- }
- foreach ($results as $result) {
- foreach ($result as $key => $value) {
- $count = count($result);
- if ($key != $count - 1) {
- file_put_contents($file, $value . ";", FILE_APPEND);
- } else {
- file_put_contents($file, $value . ";\n", FILE_APPEND);
- }
- }
- }
- echo 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement