<?php
$title[] = 'c005 - c009'; // c5-9
$title[] = 'c5.00 & c009'; // c5 & 9
$title[] = 'text c19 & c20 text'; //c19-20
$title[] = 'c19 & c20'; // c19-20
$title[] = 'text chapter 19 and chapter 25 text'; // c19 & 25
$title[] = 'text chapter 19 - chapter 23 and chapter 25 text'; // c19-23 & 25 (c19 for termless)
$title[] = 'text chapter 19 - chapter 23, chapter 25 text'; // c19-23 & 25 (c19 for termless)
$title[] = 'text chapter 23 text'; // c23
$title[] = 'text chapter 23, chapter 25-29 text'; // c23 & 25-29
$title[] = 'text chapters 23-26, 28, 29 + 30 + 32-39 text'; // c23-26 & c28-30 & c32-39
$title[] = 'text chapter 25.6 text'; // c25.6
$title[] = 'text chapters 23, 24, 25 text'; // c23-25
$title[] = 'text chapters 23+24+25 text'; // c23-25
$title[] = 'text chapter 23, 25 text'; // c23 & 25
$title[] = 'text chapter 23 & 24 & 25 text'; // c23-25
$title[] = 'text c25.5-30 text'; // c25.5-30
$title[] = 'text c99-c102 text'; // c99-102 (c99 for termless)
$title[] = 'text chapter 1 - 3 text'; // c1-3
$title[] = 'sometext 33 text chapter 1, 2 text 3'; // c1-2 or c33 if no terms
$title[] = 'text v2c5-10 text'; // c5-10 or c2 if no terms
$title[] = 'text cccc5-10 text'; // c5-10
$title[] = 'text chapters 23, 24, 25, 29, 31, 32 text'; // c23-25 & 29 & 31-32
$title[] = 'chapter 19 - chapter 23'; // c19-23 or c19 for termless
$title[] = 'chapter 12 part 2'; // c12
function get_chapter($text, $terms) {
$and = '(?: [,&+]|\band\b )';
$isrange = "(?: \s*-\s* $rterms? \s*\d+ )";
$isdotnum = '(?:\.\d+)';
$the_regexp = "/(
$rterms \s* \d+ $isdotnum? $isrange?
( \s* $and \s* $rterms? \s* \d+ $isrange? )*
)/mix";
$result['orignal'] = $text;
$result['found_match'] = $tmp = $matches[1];
));
}, $chapters);
$chapters = merge_chapters($chapters);
$result['converted'] = join_chapters($chapters);
}
else {
$result['found_match'] = '';
$result['converted'] = $text;
}
return $result;
}
function merge_chapters($chapters) {
$i = 0;
$begin = $end = -1;
foreach ($chapters as $chapter) {
// Fetch next chapter
$next = isset($chapters[$i+1]) ?
$chapters[$i+1] : -1; // If not set, set begin chapter
if ($begin == -1) {$begin = $chapter;}
// It is a range, we reset begin/end and store the range
$begin = $end = -1;
}
else if ($chapter+1 == $next) {
// next is current + 1, update end
$end = $next;
}
else {
// store result (if no end, then store current chapter, else store the range
$begin = $end = -1; // reset, since we stored results
}
$i++; // needed for $next
}
return $rtchapters;
}
function join_chapters($chapters) {
return 'c' . implode(' & ', $chapters) . "\n"; }
print "\nTERMS LEGEND:\n";
print "Case 1. = ['chapters', 'chapter', 'ch', 'c']\n";
print "Case 2. = []\n\n\n\n";
foreach ($title as $t) {
// If some patterns start by same letters, use longest first.
print "Original: $t\n";
print 'Case 1. = ';
$result = get_chapter($t, ['chapters', 'chapter', 'ch', 'c']);
print 'Case 2. = ';
$result = get_chapter($t, []);
print "--------------------------\n";
}
PD9waHAKCiR0aXRsZVtdID0gJ2MwMDUgLSBjMDA5JzsgLy8gYzUtOQokdGl0bGVbXSA9ICdjNS4wMCAmIGMwMDknOyAvLyBjNSAmIDkKJHRpdGxlW10gPSAndGV4dCBjMTkgJiBjMjAgdGV4dCc7IC8vYzE5LTIwCiR0aXRsZVtdID0gJ2MxOSAmIGMyMCc7IC8vIGMxOS0yMAokdGl0bGVbXSA9ICd0ZXh0IGNoYXB0ZXIgMTkgYW5kIGNoYXB0ZXIgMjUgdGV4dCc7IC8vIGMxOSAmIDI1CiR0aXRsZVtdID0gJ3RleHQgY2hhcHRlciAxOSAtIGNoYXB0ZXIgMjMgYW5kIGNoYXB0ZXIgMjUgdGV4dCc7IC8vIGMxOS0yMyAmIDI1IChjMTkgZm9yIHRlcm1sZXNzKQokdGl0bGVbXSA9ICd0ZXh0IGNoYXB0ZXIgMTkgLSBjaGFwdGVyIDIzLCBjaGFwdGVyIDI1IHRleHQnOyAvLyBjMTktMjMgJiAyNSAo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gJHJlc3VsdCA9IGdldF9jaGFwdGVyKCR0LCBbJ2NoYXB0ZXJzJywgJ2NoYXB0ZXInLCAnY2gnLCAnYyddKTsKICBwcmludF9yICgkcmVzdWx0KTsKICBwcmludCAnQ2FzZSAyLiA9ICc7CiAgJHJlc3VsdCA9IGdldF9jaGFwdGVyKCR0LCBbXSk7CiAgcHJpbnRfciAoJHJlc3VsdCk7CiAgcHJpbnQgIi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iOwp9Cg==
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
)
--------------------------