fork(4) download
  1. <?php
  2.  
  3. $titles = ['Using commas chapter 12, chapter 13, chapter 15, chapter 20 ther erst of the title', 'This title is an example using and chapter 12 and 24 and the rest of the text', 'This title uses also and in this way: chapter 10 and chapter 45 and chapter 46 and chapter 47 and the rest of the title', 'text chapter 25.6 text', 'text chapters 23, 24, 25 text', 'text chapters 23+24+25 text', 'text chapter 23, 25 text', 'text chapter 23 & 24 & 25 text', 'text c25.5-30 text', 'text c99-c102 text', 'text chapter 99 - chapter 102 text', 'text chapter 1 - 3 text', '33 text chapter 1, 2 text 3', 'text v2c5-10 text', 'text chapters 23, 24, 25, 29, 31, 32 text', 'this title is new to test chapter 1 - c2 rest of the title', 'complex title with many values c1 & 2 & 3 & 6 & 7 & 10 & 11 & 12 & 13 & 16 & 18 & 19 & 24 & 25 & 29 rest of the complex title'];
  4.  
  5. $model = ['chapters?', 'chap', 'c'];
  6. $c = '(?:' . implode('|', $model) . ')';
  7. $n = '\d+\.?\d*';
  8. $s = '(?:[\&\+,]|and)';
  9. $e = '[ $]';
  10.  
  11. $g1 = "$c *($n) *\- *$c *($n)$e";
  12. $g2 = "$c *($n) *\- *($n)$e";
  13. $g3 = "$c *((?:(?:$n) *$s *)+(?:$n))$e";
  14. $g4 = "((?:$c *$n *$s *)+$c *$n)$e";
  15. $g5 = "$c *($n)$e";
  16.  
  17. $reg = "/(?:$g1|$g2|$g3|$g4|$g5)/";
  18.  
  19. function getChapters ($title) {
  20.  
  21. global $n, $reg;
  22.  
  23. if (!preg_match($reg, $title, $matches)) return '';
  24.  
  25. $numbers = array_values(array_filter($matches));
  26.  
  27. if (count($numbers) == 3) return "c{$numbers[1]}-{$numbers[2]}";
  28.  
  29. if(!preg_match_all("/$n/", $numbers[1], $nmatches, PREG_PATTERN_ORDER)) return '';
  30. $m = $nmatches[0];
  31. $t = count($m);
  32. $str = "c{$m[0]}";
  33. foreach($m as $i => $mn) {
  34. if ($i == 0) continue;
  35. if ($mn == $m[$i - 1] + 1) {
  36. if (substr($str, -1) != '-') $str .= '-';
  37. if ($i == $t - 1 || $mn != $m[$i + 1] - 1) $str .= $mn;
  38. } else {
  39. if ($i < $t) $str .= ' & ';
  40. $str .= $mn;
  41. }
  42. }
  43. return $str;
  44.  
  45. }
  46.  
  47. foreach($titles as $t) {
  48. $chapters = getChapters($t);
  49. echo("$t\n");
  50. echo("($chapters)\n");
  51. echo("\n");
  52. }
  53.  
Success #stdin #stdout 0.04s 23652KB
stdin
Standard input is empty
stdout
Using commas chapter 12, chapter 13, chapter 15, chapter 20 ther erst of the title
(c12-13 & 15 & 20)

This title is an example using and chapter 12 and 24 and the rest of the text
(c12 & 24)

This title uses also and in this way: chapter 10 and chapter 45 and chapter 46 and chapter 47 and the rest of the title
(c10 & 45-47)

text chapter 25.6 text
(c25.6)

text chapters 23, 24, 25 text
(c23-25)

text chapters 23+24+25 text
(c23-25)

text chapter 23, 25 text
(c23 & 25)

text chapter 23 & 24 & 25 text
(c23-25)

text c25.5-30 text
(c25.5-30)

text c99-c102 text
(c99-102)

text chapter 99 - chapter 102 text
(c99-102)

text chapter 1 - 3 text
(c1-3)

33 text chapter 1, 2 text 3
(c1-2)

text v2c5-10 text
(c5-10)

text chapters 23, 24, 25, 29, 31, 32 text
(c23-25 & 29 & 31-32)

this title is new to test chapter 1 - c2 rest of the title
(c1-2)

complex title with many values c1 & 2 & 3 & 6 & 7 & 10 & 11 & 12 & 13 & 16 & 18 & 19 & 24 & 25 & 29 rest of the complex title
(c1-3 & 6-7 & 10-13 & 16 & 18-19 & 24-25 & 29)