fork download
  1. <?php
  2.  
  3. $title[] = 'c005 - c009'; // c5-9
  4. $title[] = 'c5.00 & c009'; // c5 & 9
  5. $title[] = 'text c19 & c20 text'; //c19-20
  6. $title[] = 'c19 & c20'; // c19-20
  7. $title[] = 'text chapter 19 and chapter 25 text'; // c19 & 25
  8. $title[] = 'text chapter 19 - chapter 23 and chapter 25 text'; // c19-23 & 25 (c19 for termless)
  9. $title[] = 'text chapter 19 - chapter 23, chapter 25 text'; // c19-23 & 25 (c19 for termless)
  10. $title[] = 'text chapter 23 text'; // c23
  11. $title[] = 'text chapter 23, chapter 25-29 text'; // c23 & 25-29
  12. $title[] = 'text chapters 23-26, 28, 29 + 30 + 32-39 text'; // c23-26 & c28-30 & c32-39
  13. $title[] = 'text chapter 25.6 text'; // c25.6
  14. $title[] = 'text chapters 23, 24, 25 text'; // c23-25
  15. $title[] = 'text chapters 23+24+25 text'; // c23-25
  16. $title[] = 'text chapter 23, 25 text'; // c23 & 25
  17. $title[] = 'text chapter 23 & 24 & 25 text'; // c23-25
  18. $title[] = 'text c25.5-30 text'; // c25.5-30
  19. $title[] = 'text c99-c102 text'; // c99-102 (c99 for termless)
  20. $title[] = 'text chapter 1 - 3 text'; // c1-3
  21. $title[] = 'sometext 33 text chapter 1, 2 text 3'; // c1-2 or c33 if no terms
  22. $title[] = 'text v2c5-10 text'; // c5-10 or c2 if no terms
  23. $title[] = 'text cccc5-10 text'; // c5-10
  24. $title[] = 'text chapters 23, 24, 25, 29, 31, 32 text'; // c23-25 & 29 & 31-32
  25. $title[] = 'chapter 19 - chapter 23'; // c19-23 or c19 for termless
  26. $title[] = 'chapter 12 part 2'; // c12
  27.  
  28. function get_chapter($text, $terms) {
  29. $rterms = sprintf('(?:%s)', implode('|', $terms));
  30.  
  31. $and = '(?: [,&+]|\band\b )';
  32. $isrange = "(?: \s*-\s* $rterms? \s*\d+ )";
  33. $isdotnum = '(?:\.\d+)';
  34. $the_regexp = "/(
  35. $rterms \s* \d+ $isdotnum? $isrange?
  36. ( \s* $and \s* $rterms? \s* \d+ $isrange? )*
  37. )/mix";
  38.  
  39. $result = array();
  40. $result['orignal'] = $text;
  41. if (preg_match($the_regexp, $text, $matches)) {
  42. $result['found_match'] = $tmp = $matches[1];
  43. $tmp = preg_replace("/$rterms\s*/i", '', $tmp);
  44. $tmp = preg_replace('/\s*-\s*/', '-', $tmp);
  45. $chapters = preg_split("/\s* $and \s*/ix", $tmp);
  46. $chapters = array_map(function($x) {
  47. return preg_replace('/\d\K\.0+/', '',
  48. preg_replace('/(?|\b0+(\d)|-\K0+(\d))/', '\1', $x
  49. ));
  50. }, $chapters);
  51. $chapters = merge_chapters($chapters);
  52. $result['converted'] = join_chapters($chapters);
  53. }
  54. else {
  55. $result['found_match'] = '';
  56. $result['converted'] = $text;
  57. }
  58. return $result;
  59. }
  60.  
  61. function merge_chapters($chapters) {
  62. $i = 0;
  63. $begin = $end = -1;
  64. $rtchapters = array();
  65. foreach ($chapters as $chapter) {
  66. // Fetch next chapter
  67. $next = isset($chapters[$i+1]) ? $chapters[$i+1] : -1;
  68. // If not set, set begin chapter
  69. if ($begin == -1) {$begin = $chapter;}
  70. if (preg_match('/-/', $chapter)) {
  71. // It is a range, we reset begin/end and store the range
  72. $begin = $end = -1;
  73. array_push($rtchapters, $chapter);
  74. }
  75. else if ($chapter+1 == $next) {
  76. // next is current + 1, update end
  77. $end = $next;
  78. }
  79. else {
  80. // store result (if no end, then store current chapter, else store the range
  81. array_push($rtchapters, sprintf('%s', $end == -1 ? $chapter : "$begin-$end"));
  82. $begin = $end = -1; // reset, since we stored results
  83. }
  84. $i++; // needed for $next
  85. }
  86. return $rtchapters;
  87. }
  88.  
  89. function join_chapters($chapters) {
  90. return 'c' . implode(' & ', $chapters) . "\n";
  91. }
  92.  
  93. print "\nTERMS LEGEND:\n";
  94. print "Case 1. = ['chapters', 'chapter', 'ch', 'c']\n";
  95. print "Case 2. = []\n\n\n\n";
  96. foreach ($title as $t) {
  97. // If some patterns start by same letters, use longest first.
  98. print "Original: $t\n";
  99. print 'Case 1. = ';
  100. $result = get_chapter($t, ['chapters', 'chapter', 'ch', 'c']);
  101. print_r ($result);
  102. print 'Case 2. = ';
  103. $result = get_chapter($t, []);
  104. print_r ($result);
  105. print "--------------------------\n";
  106. }
  107.  
Success #stdin #stdout 0.01s 24056KB
stdin
Standard input is empty
stdout
TERMS LEGEND:
Case 1. = ['chapters', 'chapter', 'ch', 'c']
Case 2. = []



Original: c005 - c009
Case 1. = Array
(
    [orignal] => c005 - c009
    [found_match] => c005 - c009
    [converted] => c5-9

)
Case 2. = Array
(
    [orignal] => c005 - c009
    [found_match] => 005
    [converted] => c5

)
--------------------------
Original: c5.00 & c009
Case 1. = Array
(
    [orignal] => c5.00 & c009
    [found_match] => c5.00 & c009
    [converted] => c5 & 9

)
Case 2. = Array
(
    [orignal] => c5.00 & c009
    [found_match] => 5.00
    [converted] => c5

)
--------------------------
Original: text c19 & c20 text
Case 1. = Array
(
    [orignal] => text c19 & c20 text
    [found_match] => c19 & c20
    [converted] => c19-20

)
Case 2. = Array
(
    [orignal] => text c19 & c20 text
    [found_match] => 19
    [converted] => c19

)
--------------------------
Original: c19 & c20
Case 1. = Array
(
    [orignal] => c19 & c20
    [found_match] => c19 & c20
    [converted] => c19-20

)
Case 2. = Array
(
    [orignal] => c19 & c20
    [found_match] => 19
    [converted] => c19

)
--------------------------
Original: text chapter 19 and chapter 25 text
Case 1. = Array
(
    [orignal] => text chapter 19 and chapter 25 text
    [found_match] => chapter 19 and chapter 25
    [converted] => c19 & 25

)
Case 2. = Array
(
    [orignal] => text chapter 19 and chapter 25 text
    [found_match] =>  19
    [converted] => c19

)
--------------------------
Original: text chapter 19 - chapter 23 and chapter 25 text
Case 1. = Array
(
    [orignal] => text chapter 19 - chapter 23 and chapter 25 text
    [found_match] => chapter 19 - chapter 23 and chapter 25
    [converted] => c19-23 & 25

)
Case 2. = Array
(
    [orignal] => text chapter 19 - chapter 23 and chapter 25 text
    [found_match] =>  19
    [converted] => c19

)
--------------------------
Original: text chapter 19 - chapter 23, chapter 25 text
Case 1. = Array
(
    [orignal] => text chapter 19 - chapter 23, chapter 25 text
    [found_match] => chapter 19 - chapter 23, chapter 25
    [converted] => c19-23 & 25

)
Case 2. = Array
(
    [orignal] => text chapter 19 - chapter 23, chapter 25 text
    [found_match] =>  19
    [converted] => c19

)
--------------------------
Original: text chapter 23 text
Case 1. = Array
(
    [orignal] => text chapter 23 text
    [found_match] => chapter 23
    [converted] => c23

)
Case 2. = Array
(
    [orignal] => text chapter 23 text
    [found_match] =>  23
    [converted] => c23

)
--------------------------
Original: text chapter 23, chapter 25-29 text
Case 1. = Array
(
    [orignal] => text chapter 23, chapter 25-29 text
    [found_match] => chapter 23, chapter 25-29
    [converted] => c23 & 25-29

)
Case 2. = Array
(
    [orignal] => text chapter 23, chapter 25-29 text
    [found_match] =>  23
    [converted] => c23

)
--------------------------
Original: text chapters 23-26, 28, 29 + 30 + 32-39 text
Case 1. = Array
(
    [orignal] => text chapters 23-26, 28, 29 + 30 + 32-39 text
    [found_match] => chapters 23-26, 28, 29 + 30 + 32-39
    [converted] => c23-26 & 28-30 & 32-39

)
Case 2. = Array
(
    [orignal] => text chapters 23-26, 28, 29 + 30 + 32-39 text
    [found_match] =>  23-26, 28, 29 + 30 + 32-39
    [converted] => c23-26 & 28-30 & 32-39

)
--------------------------
Original: text chapter 25.6 text
Case 1. = Array
(
    [orignal] => text chapter 25.6 text
    [found_match] => chapter 25.6
    [converted] => c25.6

)
Case 2. = Array
(
    [orignal] => text chapter 25.6 text
    [found_match] =>  25.6
    [converted] => c25.6

)
--------------------------
Original: text chapters 23, 24, 25 text
Case 1. = Array
(
    [orignal] => text chapters 23, 24, 25 text
    [found_match] => chapters 23, 24, 25
    [converted] => c23-25

)
Case 2. = Array
(
    [orignal] => text chapters 23, 24, 25 text
    [found_match] =>  23, 24, 25
    [converted] => c23-25

)
--------------------------
Original: text chapters 23+24+25 text
Case 1. = Array
(
    [orignal] => text chapters 23+24+25 text
    [found_match] => chapters 23+24+25
    [converted] => c23-25

)
Case 2. = Array
(
    [orignal] => text chapters 23+24+25 text
    [found_match] =>  23+24+25
    [converted] => c23-25

)
--------------------------
Original: text chapter 23, 25 text
Case 1. = Array
(
    [orignal] => text chapter 23, 25 text
    [found_match] => chapter 23, 25
    [converted] => c23 & 25

)
Case 2. = Array
(
    [orignal] => text chapter 23, 25 text
    [found_match] =>  23, 25
    [converted] => c23 & 25

)
--------------------------
Original: text chapter 23 & 24 & 25 text
Case 1. = Array
(
    [orignal] => text chapter 23 & 24 & 25 text
    [found_match] => chapter 23 & 24 & 25
    [converted] => c23-25

)
Case 2. = Array
(
    [orignal] => text chapter 23 & 24 & 25 text
    [found_match] =>  23 & 24 & 25
    [converted] => c23-25

)
--------------------------
Original: text c25.5-30 text
Case 1. = Array
(
    [orignal] => text c25.5-30 text
    [found_match] => c25.5-30
    [converted] => c25.5-30

)
Case 2. = Array
(
    [orignal] => text c25.5-30 text
    [found_match] => 25.5-30
    [converted] => c25.5-30

)
--------------------------
Original: text c99-c102 text
Case 1. = Array
(
    [orignal] => text c99-c102 text
    [found_match] => c99-c102
    [converted] => c99-102

)
Case 2. = Array
(
    [orignal] => text c99-c102 text
    [found_match] => 99
    [converted] => c99

)
--------------------------
Original: text chapter 1 - 3 text
Case 1. = Array
(
    [orignal] => text chapter 1 - 3 text
    [found_match] => chapter 1 - 3
    [converted] => c1-3

)
Case 2. = Array
(
    [orignal] => text chapter 1 - 3 text
    [found_match] =>  1 - 3
    [converted] => c1-3

)
--------------------------
Original: sometext 33 text chapter 1, 2 text 3
Case 1. = Array
(
    [orignal] => sometext 33 text chapter 1, 2 text 3
    [found_match] => chapter 1, 2
    [converted] => c1-2

)
Case 2. = Array
(
    [orignal] => sometext 33 text chapter 1, 2 text 3
    [found_match] =>  33
    [converted] => c33

)
--------------------------
Original: text v2c5-10 text
Case 1. = Array
(
    [orignal] => text v2c5-10 text
    [found_match] => c5-10
    [converted] => c5-10

)
Case 2. = Array
(
    [orignal] => text v2c5-10 text
    [found_match] => 2
    [converted] => c2

)
--------------------------
Original: text cccc5-10 text
Case 1. = Array
(
    [orignal] => text cccc5-10 text
    [found_match] => c5-10
    [converted] => c5-10

)
Case 2. = Array
(
    [orignal] => text cccc5-10 text
    [found_match] => 5-10
    [converted] => c5-10

)
--------------------------
Original: text chapters 23, 24, 25, 29, 31, 32 text
Case 1. = Array
(
    [orignal] => text chapters 23, 24, 25, 29, 31, 32 text
    [found_match] => chapters 23, 24, 25, 29, 31, 32
    [converted] => c23-25 & 29 & 31-32

)
Case 2. = Array
(
    [orignal] => text chapters 23, 24, 25, 29, 31, 32 text
    [found_match] =>  23, 24, 25, 29, 31, 32
    [converted] => c23-25 & 29 & 31-32

)
--------------------------
Original: chapter 19 - chapter 23
Case 1. = Array
(
    [orignal] => chapter 19 - chapter 23
    [found_match] => chapter 19 - chapter 23
    [converted] => c19-23

)
Case 2. = Array
(
    [orignal] => chapter 19 - chapter 23
    [found_match] =>  19
    [converted] => c19

)
--------------------------
Original: chapter 12 part 2
Case 1. = Array
(
    [orignal] => chapter 12 part 2
    [found_match] => chapter 12
    [converted] => c12

)
Case 2. = Array
(
    [orignal] => chapter 12 part 2
    [found_match] =>  12
    [converted] => c12

)
--------------------------