fork(1) download
  1. <?php
  2.  
  3.  
  4. $regexp = "/(([а-я]+[a-z]+)|([a-z]+[а-я]+))\\w*/ui";//Expression for searching latin letters among cyrillics in words
  5. $wordRegexp = "/[a-z]/ui"; //Expression for pick out latin letter and replaced by cyrillic
  6. $string = "Пocтaвкa мяco гoвядины, бecкостнoe для нужд государственного бюджетного учреждения здравоохранения Республики Башкортостан Инфекционная клиническая больница № 4 города Уфа \n";
  7. $matches = []; // array for pick words with latin letters
  8. $subjects = [];// array for replaced latin letters
  9. $wordTemplate = array(
  10. 'a' => 'а',
  11. 'c' => 'с',
  12. 'o' => 'о',
  13. 'e' => 'е',
  14. 'y' => 'у',
  15. 'x' => 'х',
  16. 'p' => 'р',
  17. );//array with replacement template
  18. preg_match_all($regexp, $string, $matches); // pick out words
  19. $matches = $matches[0]; // All words are located in 0 array in array matches that operation rewrite array.
  20. for ($i=(count($matches)-1); $i>=0; $i--) {
  21. $problemWord = $matches[$i];
  22. echo "Line: $problemWord \n"; // just for check
  23. preg_match_all($wordRegexp, $problemWord, $subjects);
  24. $subjects = $subjects[0];// operation like in 20 line
  25. for ($j=(count($subjects)-1); $j>=0; $j--) { // pick out latin letters
  26. $problemLetter = $subjects[$j];
  27. echo "Line_subjects: $problemLetter \n";// just for check
  28. foreach ($wordTemplate as $key => $value) { // searching similar letter in tamplate
  29. if ($key == $problemLetter) {
  30. $letterRegexp = "/$key/ui";
  31. $problemWord = preg_replace($letterRegexp, "[{$value}]", $problemWord);
  32. }
  33. }
  34. }
  35. echo "problemWord after foreach: $problemWord \n";
  36. }
  37. ?>
Success #stdin #stdout 0s 82624KB
stdin
Standard input is empty
stdout
Line: бecкостнoe 
Line_subjects: e 
Line_subjects: o 
Line_subjects: c 
Line_subjects: e 
problemWord after foreach: б[е][с]костн[о][е] 
Line: гoвядины 
Line_subjects: o 
problemWord after foreach: г[о]вядины 
Line: мяco 
Line_subjects: o 
Line_subjects: c 
problemWord after foreach: мя[с][о] 
Line: Пocтaвкa 
Line_subjects: a 
Line_subjects: a 
Line_subjects: c 
Line_subjects: o 
problemWord after foreach: П[о][с]т[а]вк[а]