Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.15 KB | None | 0 0
  1. <?php
  2.  
  3. $utf_csv = array_map('str_getcsv', file('123.csv'));
  4.  
  5. //убираем первый элемент массива - заголовки
  6. array_shift($utf_csv);
  7.  
  8. $file = 'new_all.csv';
  9.  
  10. function getPercent($title) {
  11.     $percent = '';
  12.  
  13.     if (preg_match_all('/\d*[.,]?\d+(\s)?(%)/ui', $title, $matches, PREG_PATTERN_ORDER)) {
  14.         if (mb_substr_count($title, 'скидка') > 0) {
  15.             if (array_key_exists(1, $matches[0])) {
  16.                 $percent = array_shift($matches[0]);
  17.             }
  18.         } else {
  19.             $percent = array_pop($matches[0]);
  20.         }
  21.  
  22.         $percent = str_replace('.', ',', $percent);
  23.  
  24.         if (preg_match('/(\d*[.,]?\d+)(\s)(%)/ui', $percent)) {
  25.             $percent = preg_replace('/(\d*[.,]?\d+)(\s)(%)/ui','$1' . '$3', $percent);
  26.         }
  27.     }
  28.  
  29.     return trim($percent);
  30. }
  31.  
  32. function getDosage($title) {
  33.     $dosage = '';
  34.  
  35.     if (preg_match('/\d+(\s)?(доз([аы])?)\b/ui', $title, $matches)) {
  36.         $dosage = $matches[0];
  37.         $dosage = str_replace(' ', '', $dosage);
  38.     }
  39.  
  40.     return trim($dosage);
  41. }
  42.  
  43. function getQuantity($title) {
  44.     $quantity = '';
  45.  
  46.     if (preg_match_all('/\D(№|Х|X|N)(\s)?\d+((\s)?(№|Х|X|N)\d+)?\b/ui', $title, $matches, PREG_PATTERN_ORDER)) {
  47.  
  48.         $quantity = array_pop($matches[0]);
  49.  
  50.         if (preg_match('/(Х|X|N)/ui', $quantity)) {
  51.             $quantity = preg_replace('/(Х|X|N)/ui','№', $quantity);
  52.         }
  53.  
  54.         if (preg_match('/(№)(\s)(\d*[.,]?\d+)/ui', $quantity)) {
  55.             $quantity = preg_replace('/(№)(\s)(\d*[.,]?\d+)/ui','$1' . '$3', $quantity);
  56.         }
  57.  
  58. //        if (preg_match('/(№)(\d*[.,]?\d+)(№)(\d*[.,]?\d+)/ui', $title)) {
  59. //            $title = preg_replace('/(№)(\d*[.,]?\d+)(№)(\d*[.,]?\d+)/ui','$1' . '$2' . ' ' . '$3' . '$4', $title);
  60. //        }
  61.     }
  62.  
  63.     return trim($quantity);
  64. }
  65.  
  66. function getUnit($title) {
  67.     $unit = '';
  68.  
  69.     if (preg_match('/\d*[.,]?\d+(\s|\/)?(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|ле|кв\W+см)\b(\/)?(\d+)?(мг|мл|г|доз([аы])?)?(([\w+\W+]{1,})\d*[.,]?\d+(\s|\/)?(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см|доз([аы])?)(\/)?(\d+)?(мг|мл|г|доз([аы])?)?)?/ui', $title, $matches)) {
  70.         $unit = $matches[0];
  71.  
  72. //        if (mb_substr_count($title, 'флуимуцил антибиотик') > 0) {
  73. //            echo 1;
  74. //        }
  75.     }
  76.  
  77. //    if (preg_match('/\s\d*[.,]?\d+(\s)?(\+)(\s)?\d*[.,]?\d+\s/ui', $unit, $matches)) {
  78. //        $unit = $matches[0];
  79. //    }
  80. //
  81. //    if (preg_match('/(\d*[.,]?\d+)(\s)(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см)/ui', $unit, $matches)) {
  82. //        $unit = preg_replace('/(\d*[.,]?\d+)(\s)(мг|мл|гр|г|мкг|ед|тыс\W+ме|тыс\W+ед|тыс|ме|кв\W+см)/ui','$1' . '$3', $unit);
  83. //    }
  84.  
  85.     return $unit;
  86. }
  87.  
  88. function getForm($title) {
  89.     $form = '';
  90.  
  91. //    if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|спрей|спр|капсулы|капс|крем|гель|мазь|суппозитории|супп|сусп|лак|табл|таб|порошок|пор)(\.)?\b/ui', $title, $matches)) {
  92.     if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|крем|спрей|спр|суппозитории|супп|суспензия|сусп|лак|порошок|пор|аэрозоль|аэр)(\.)?\b/ui', $title, $matches)) {
  93.         $form = $matches[0];
  94.     }
  95.  
  96.     if (preg_match('/\b(раст|р-р|р-в|драже|сироп|капли|крем|спрей|спр|суппозитории|супп|суспензия|сусп|лак|порошок|пор|аэрозоль|аэр|свечи|настойка|пастилки|гран|саше)(\.)?\b/ui', $title, $matches)) {
  97.         $form = $matches[0];
  98.     }
  99.  
  100.     if (preg_match('/(раст|р-р|р-в)(\.)?/ui', $form, $matches)) {
  101.         $form = preg_replace('/(раст|р-р|р-в)(\.)?/ui','раствор', $form);
  102.     }
  103.  
  104.     if (preg_match('/(спрей|спр)(\.)?/ui', $form, $matches)) {
  105.         $form = preg_replace('/(спрей|спр)(\.)?[^а-я]/ui','спрей', $form);
  106.     }
  107.  
  108. //    if (preg_match('/(капсулы|капс)(\.)?/ui', $form, $matches)) {
  109. //        $form = preg_replace('/(капсулы|капс)(\.)?/ui','капс.', $form);
  110. //    }
  111.  
  112.     if (preg_match('/(суппозитории|супп)(\.)?/ui', $form, $matches)) {
  113.         $form = preg_replace('/(суппозитории|супп)(\.)?/ui','суппозитории', $form);
  114.     }
  115.  
  116.     if (preg_match('/(суспензия|сусп)(\.)?/ui', $form, $matches)) {
  117.         $form = preg_replace('/(суспензия|сусп)(\.)?/ui','суспензия', $form);
  118.     }
  119.  
  120. //    if (preg_match('/(таб)(\.)?/ui', $form, $matches)) {
  121. //        $form = preg_replace('/(таб)(\.)?/ui','таб.', $form);
  122. //    }
  123.  
  124.     if (preg_match('/(порошок|пор)(\.)?/ui', $form, $matches)) {
  125.         $form = preg_replace('/(порошок|пор)(\.)?/ui', 'порошок', $form);
  126.     }
  127.  
  128.     if (preg_match('/(аэрозоль|аэр)(\.)?/ui', $form, $matches)) {
  129.         $form = preg_replace('/(аэрозоль|аэр)(\.)?/ui', 'аэрозоль', $form);
  130.     }
  131.  
  132.     $form = str_replace('..', '.', $form);
  133.  
  134.     return trim($form);
  135. }
  136.  
  137. function forChildren($title) {
  138.     $boolean = 0;
  139.  
  140.     if (preg_match('/\b(дет)(\.)?(\s)?/ui', $title, $matches)) {
  141.         $boolean = 1;
  142.     }
  143.  
  144.     return $boolean;
  145. }
  146.  
  147. function getPackage($title) {
  148.     $package = '';
  149.  
  150.     if (preg_match('/\b(амп|туб[аы]?|фл)(\.)?(\s)?/ui', $title, $matches)) {
  151.         $package = $matches[0];
  152.     }
  153.  
  154.     $package = str_replace('амп', 'амп.', $package);
  155.  
  156.     $package = str_replace('фл', 'фл.', $package);
  157.  
  158.     $package = str_replace('..', '.', $package);
  159.  
  160.     return trim($package);
  161. }
  162.  
  163. $drug = [];
  164. $drugs = [];
  165.  
  166. $results = [];
  167.  
  168. foreach ($utf_csv as $utf_row) {
  169.  
  170.     $true_title = mb_strtolower($utf_row[1]);
  171.     $true_title = str_replace('   ', ' ', $true_title);
  172.     $true_title = str_replace('  ', ' ', $true_title);
  173.  
  174.     $percent = getPercent($true_title);
  175.     $dosage = getDosage($true_title);
  176.     $package = getPackage($true_title);
  177.     $unit = getUnit($true_title);
  178.     $quantity = getQuantity($true_title);
  179.     $form = getForm($true_title);
  180.     $forChildren = forChildren($true_title);
  181.  
  182.     $edit_title = $utf_row[2];
  183.     $edit_title = str_replace('для детей', '', $edit_title);
  184.     $edit_title = str_replace('   ', ' ', $edit_title);
  185.     $edit_title = str_replace('  ', ' ', $edit_title);
  186.  
  187.  
  188.     if (preg_match('/(раст|р-р|р-в)(\.)?/ui', $edit_title, $matches)) {
  189.         $edit_title = preg_replace('/(раст|р-р|р-в)(\.)?/ui','', $edit_title);
  190.     }
  191.  
  192.     if (preg_match('/(спрей|спр)(\.)?/ui', $edit_title, $matches)) {
  193.         $edit_title = preg_replace('/(спрей|спр)(\.)?[^а-я]/ui','', $edit_title);
  194.  
  195.         $edit_title = str_replace(' спрей', '', $edit_title);
  196.     }
  197.  
  198.     if (preg_match('/(суппозитории|супп)(\.)?/ui', $edit_title, $matches)) {
  199.         $edit_title = preg_replace('/(суппозитории|супп)(\.)?/ui','', $edit_title);
  200.     }
  201.  
  202.     if (preg_match('/(суспензия|сусп)(\.)?/ui', $edit_title, $matches)) {
  203.         $edit_title = preg_replace('/(суспензия|сусп)(\.)?/ui','', $edit_title);
  204.     }
  205.  
  206.     if (preg_match('/(порошок|пор)(\.)?/ui', $edit_title, $matches)) {
  207.         $edit_title = preg_replace('/(порошок|пор)(\.)?/ui', '', $edit_title);
  208.     }
  209.  
  210.     if (preg_match('/(аэрозоль|аэр)(\.)?/ui', $edit_title, $matches)) {
  211.         $edit_title = preg_replace('/(аэрозоль|аэр)(\.)?/ui', '', $edit_title);
  212.     }
  213.  
  214.     if (preg_match('/(саше)(\.)?/ui', $form, $matches)) {
  215.         $form = preg_replace('/(саше)(\.)?/ui', '', $form);
  216.     }
  217.  
  218.  
  219.     $edit_title = trim($edit_title);
  220.  
  221.     $result = [];
  222.  
  223. //    $drug = [];
  224.  
  225. //    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]);
  226. //    array_push($drugs, $drug);
  227.  
  228. //    if (preg_match('/(амп)/ui', $true_title, $matches)) {
  229. //        if (mb_substr_count($true_title,'р-ра') > 0) {
  230. //            array_push($drug, $true_title, $percent, $dosage, $unit, $quantity, $package, $form, $forChildren);
  231. //        }
  232. //    }
  233.  
  234.     if ($utf_row[4] == '') {
  235.         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],
  236.             $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]);
  237.     } else {
  238.         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],
  239.             $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]);
  240.     }
  241.  
  242.     array_push($results, $result);
  243. }
  244.  
  245. unset($a);
  246. unset($b);
  247.  
  248. //unlink($file);
  249.  
  250. $blas = [];
  251. $bla = [];
  252.  
  253. array_push($bla, 'NAME_ID', 'Full name', 'Name', 'Для детей', 'Форма выпуска', 'Граммовка', 'Количество', 'Способ применения', 'Проценты', 'Доза', 'Вкус', 'Упаковка', 'Остаток', 'ШК', 'Страна', 'Производитель', 'Цена факт', 'Для сайта');
  254. array_push($blas, $bla);
  255.  
  256. foreach ($blas as $result) {
  257.     foreach ($result as $key => $value) {
  258.         $count = count($result);
  259.         if ($key != $count - 1) {
  260.             file_put_contents($file, $value . ";", FILE_APPEND);
  261.         } else {
  262.             file_put_contents($file, $value . ";\n", FILE_APPEND);
  263.         }
  264.     }
  265. }
  266.  
  267. foreach ($results as $result) {
  268.     foreach ($result as $key => $value) {
  269.         $count = count($result);
  270.         if ($key != $count - 1) {
  271.             file_put_contents($file, $value . ";", FILE_APPEND);
  272.         } else {
  273.             file_put_contents($file, $value . ";\n", FILE_APPEND);
  274.         }
  275.     }
  276. }
  277.  
  278. echo 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement