- preg_match: documentation ( source)
- range: documentation ( source)
<?php
$pattern = "/^([A-Z\d]+)\[(?|(\d+)-(\d+)|([A-Z])-([A-Z]))]|([A-Z\d]+)$/";
$strings = [
"SW[1-3]",
"LD1[A-D]",
"LD1"
];
foreach ($strings as $s) {
if (preg_match($pattern, $s, $match)) {
if (array_key_exists(4, $match)) {
echo $match[4] . PHP_EOL;
continue;
}
foreach (range($match[2], $match[3]) as $m) {
echo $match[1] . $m . PHP_EOL;
}
}
}