<?php
$strs = ['c0', 'c0-3', 'c0+3', 'c0 & 9', 'c0001, 2, 03', 'c01-03', 'c1.0 - 2.0', 'chapter 2A Hello', 'chapter 2AHello', 'chapter 10.4c', 'chapter 2B', 'episode 23.000 & 00024', 'episode 23 & 24', 'e23 & 24', 'text c25.6 text', '001 & 2 & 5 & 8-20 & 100 text chapter 25.6 text 98', 'hello 23 & 24', 'ep 1 - 2', 'chapter 1 - chapter 2', 'text chapter 25.6 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 1 - 3 text', '33 text chapter 1, 2 text 3','text chapters 23, 24, 25, 29, 31, 32 text', 'c19 & c20', 'chapter 25.6 & chapter 29', 'chapter 25+c26', 'chapter 25 + 26 + 27'];
$terms = ['episode', 'chapter', 'ch', 'ep', 'c', 'e', ''];
usort($terms, function($a, $b) { });
return strlen($term) > 0 ?
"(" . substr($term, 0, 1) . ")(" . substr($term, 1) . "s?)": "()()" ;}, $terms)) . ")\s*";
$terms)) . ")\s*";
$reg = "~$chapter_main_rx((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:$chapter_aux_rx)?(?4))*)~ui";
foreach ($strs as $s) {
"~(\d*(?:\.\d+)?)([A-Z]?)\s*-\s*(?:$chapter_aux_rx)?|(\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?(?1))*~ui", function($x) use ($chapter_aux_rx) {
return (isset($x[3]) && strlen($x[3])) ? buildNumChain
(preg_split("~\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?~ui", $x[0])) }, $m[3]);
print_r(["original" => $s, "found_match" => trim($m[0]), "converted" => $m[1] . $p3]); echo "\n";
} else {
echo "No match for '$s'!\n";
}
}
function buildNumChain($arr) {
$ret = "";
$rngnum = "";
for ($i=0; $i < count($arr); $i++) { $val = $arr[$i];
$part = "";
if (preg_match('~^(\d+(?:\.\d+)?)([A-Z]?)$~i', $val, $ms)) { $val = $ms[1];
}
}
$val = $val + 0;
if (($i < count($arr) - 1) && $val == ($arr[$i+1] + 0) - 1) { $ret .= ($i == 0 ? "" : " & ") . $val;
}
$rngnum = $val;
} else if (!empty($rngnum) || $i == count($arr)) { $ret .= '-' . $val;
$rngnum = "";
} else {
$ret .= ($i == 0 ? "" : " & ") . $val . $part;
}
}
return $ret;
}
PD9waHAKCiRzdHJzID0gWydjMCcsICdjMC0zJywgJ2MwKzMnLCAnYzAgJiA5JywgJ2MwMDAxLCAyLCAwMycsICdjMDEtMDMnLCAnYzEuMCAtIDIuMCcsICdjaGFwdGVyIDJBIEhlbGxvJywgJ2NoYXB0ZXIgMkFIZWxsbycsICdjaGFwdGVyIDEwLjRjJywgJ2NoYXB0ZXIgMkInLCAnZXBpc29kZSAyMy4wMDAgJiAwMDAyNCcsICdlcGlzb2RlIDIzICYgMjQnLCAnZTIzICYgMjQnLCAndGV4dCBjMjUuNiB0ZXh0JywgJzAwMSAmIDIgJiA1ICYgOC0yMCAmIDEwMCB0ZXh0IGNoYXB0ZXIgMjUuNiB0ZXh0IDk4JywgJ2hlbGxvIDIzICYgMjQnLCAnZXAgMSAtIDInLCAnY2hhcHRlciAxIC0gY2hhcHRlciAyJywgJ3RleHQgY2hhcHRlciAyNS42IHRleHQnLCAndGV4dCBjaGFwdGVycyAyMywgMjQsIDI1IHRleHQnLCd0ZXh0IGNoYXB0ZXIgMjMsIDI1IHRleHQnLCAndGV4dCBjaGFwdGVyIDIzICYgMjQgJiAyNSB0ZXh0JywndGV4dCBjMjUuNS0zMCB0ZXh0JywgJ3RleHQgYzk5LWMxMDIgdGV4dCcsICd0ZXh0IGNoYXB0ZXIgMSAtIDMgdGV4dCcsICczMyB0ZXh0IGNoYXB0ZXIgMSwgMiB0ZXh0IDMnLCd0ZXh0IGNoYXB0ZXJzIDIzLCAyNCwgMjUsIDI5LCAzMSwgMzIgdGV4dCcsICdjM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ykkfmknLCAkdmFsLCAkbXMpKSB7CiAgICAgICAgCSR2YWwgPSAkbXNbMV07CiAgICAgICAgCWlmICghZW1wdHkoJG1zWzJdKSkgewogICAgICAgIAkJJHBhcnQgPSAnIHBhcnQgJyAuIChvcmQoc3RydG9sb3dlcigkbXNbMl0pKSAtIDk2KTsKICAgICAgICAJfQogICAgICAgIH0KICAgICAgICAkdmFsID0gJHZhbCArIDA7CiAgICAgICAgaWYgKCgkaSA8IGNvdW50KCRhcnIpIC0gMSkgJiYgJHZhbCA9PSAoJGFyclskaSsxXSArIDApIC0gMSkgewogICAgICAgICAgICBpZiAoZW1wdHkoJHJuZ251bSkpICB7CiAgICAgICAgICAgICAgICAkcmV0IC49ICgkaSA9PSAwID8gIiIgOiAiICYgIikgLiAkdmFsOwogICAgICAgICAgICB9CiAgICAgICAgICAgICRybmdudW0gPSAkdmFsOwogICAgICAgIH0gZWxzZSBpZiAoIWVtcHR5KCRybmdudW0pIHx8ICRpID09IGNvdW50KCRhcnIpKSB7CiAgICAgICAgICAgICRyZXQgLj0gJy0nIC4gJHZhbDsKICAgICAgICAgICAgJHJuZ251bSA9ICIiOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICRyZXQgLj0gKCRpID09IDAgPyAiIiA6ICIgJiAiKSAuICR2YWwgLiAkcGFydDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gJHJldDsKfQo=