fork download
  1. <?php
  2.  
  3. function doOneStep($mask, $filename, $symbol)
  4. {
  5. if ((mb_strlen($mask) == 0) && (mb_strlen($filename) == 0)) {
  6. return true;
  7. }
  8.  
  9. $lMask = mb_substr($mask, 0, 1);
  10. $lFilename = mb_substr($filename, 0, 1);
  11.  
  12. /**
  13.   * Если в маске символ "?", а длина имени файла равна нулю -> проиграли,
  14.   * если больше нуля, то убираем один символ из имени файла
  15.   */
  16. if ($lMask == '?') {
  17. if (mb_strlen($filename) > 0) {
  18. return doOneStep(mb_substr($mask, 1), mb_substr($filename, 1), $lMask);
  19. } else {
  20. return false;
  21. }
  22. }
  23.  
  24. if ($lMask == '*') {
  25. return doOneStep(mb_substr($mask, 1), $filename, $lMask);
  26. }
  27.  
  28. /**
  29.   * Если символ маски и имени файла совпадают -> убираем по симовлу
  30.   * иначе, если пред. символ '*' и длина имени файла != 0 -> убираем символ имени файла
  31.   */
  32. if ($lMask == $lFilename) {
  33. return doOneStep(mb_substr($mask, 1), mb_substr($filename, 1), $symbol);
  34. } else {
  35. if (mb_strlen($filename) > 0 && $symbol == '*') {
  36. return doOneStep($mask, mb_substr($filename, 1), $symbol);
  37. } else {
  38. return false;
  39. }
  40. }
  41. }
  42.  
  43. $mask = 'test';
  44. $filename = 't';
  45. $test = array(
  46. 'mask' => array('test', 'test', 'test', 'te?t', 'te?t', 'te?t', 'te?t', 't???', 't???', 't*', 't*', 't*', '*t' , '*t***t*', '*t***t*', 't*st', '*t*st', '', '', 't', '*', 'te*??'),
  47. 'filename' => array('test', 'test.txt', 't', 'test', 'tesst', 'tet', 'te?t', 'test', 't', 'test', 't', 'rest', 'rest', 'test', 'rest', 'teskest', 'teskest', '', 't', '', '', 'tennis'),
  48. 'answer' => array('+', '-', '-', '+', '-', '-', '+', '+', '-', '+', '+', '-', '+', '+', '-', '+', '+', '+', '-', '-', '+', '+')
  49. );
  50.  
  51. function addSpaceCenter($text, $length)
  52. {
  53. if (mb_strlen($text) < $length) {
  54. $text = str_repeat(' ', round(($length - mb_strlen($text)) / 2)) . $text . str_repeat(' ', floor(($length - mb_strlen($text)) / 2));
  55. }
  56. return $text;
  57. }
  58.  
  59. function addDash($width, $cols) {
  60. echo str_repeat('-', $width*$cols + $cols) . "\n";
  61. }
  62.  
  63. $countTest = count($test['mask']);
  64.  
  65. $width = 15;
  66. echo addSpaceCenter('Имя файла', $width). "|" . addSpaceCenter('Маска', $width) . "|" . addSpaceCenter('Результат', $width) . "" . addSpaceCenter('Правильный ответ', $width) . "|\n";
  67. addDash($width, 4);
  68. for ($i = 0; $i < $countTest; $i++) {
  69. $answer = doOneStep($test['mask'][$i], $test['filename'][$i], '') ? "+" : "-";
  70. echo addSpaceCenter($test['filename'][$i], $width) . "|" . addSpaceCenter($test['mask'][$i], $width) . "|" . addSpaceCenter($answer, $width) . "|" . addSpaceCenter($test['answer'][$i], $width) . "|\n";
  71. addDash($width, 4);
  72. }
  73.  
  74.  
  75. ?>
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
   Имя файла   |     Маска     |   Результат   Правильный ответ|
----------------------------------------------------------------
      test     |      test     |       +       |       +       |
----------------------------------------------------------------
    test.txt   |      test     |       -       |       -       |
----------------------------------------------------------------
       t       |      test     |       -       |       -       |
----------------------------------------------------------------
      test     |      te?t     |       +       |       +       |
----------------------------------------------------------------
     tesst     |      te?t     |       -       |       -       |
----------------------------------------------------------------
      tet      |      te?t     |       -       |       -       |
----------------------------------------------------------------
      te?t     |      te?t     |       +       |       +       |
----------------------------------------------------------------
      test     |      t???     |       +       |       +       |
----------------------------------------------------------------
       t       |      t???     |       -       |       -       |
----------------------------------------------------------------
      test     |       t*      |       +       |       +       |
----------------------------------------------------------------
       t       |       t*      |       +       |       +       |
----------------------------------------------------------------
      rest     |       t*      |       -       |       -       |
----------------------------------------------------------------
      rest     |       *t      |       +       |       +       |
----------------------------------------------------------------
      test     |    *t***t*    |       +       |       +       |
----------------------------------------------------------------
      rest     |    *t***t*    |       -       |       -       |
----------------------------------------------------------------
    teskest    |      t*st     |       +       |       +       |
----------------------------------------------------------------
    teskest    |     *t*st     |       +       |       +       |
----------------------------------------------------------------
               |               |       +       |       +       |
----------------------------------------------------------------
       t       |               |       -       |       -       |
----------------------------------------------------------------
               |       t       |       -       |       -       |
----------------------------------------------------------------
               |       *       |       +       |       +       |
----------------------------------------------------------------
     tennis    |     te*??     |       -       |       +       |
----------------------------------------------------------------