- preg_match: documentation ( source)
- print_r: documentation ( source)
- preg_match_all: documentation ( source)
- explode: documentation ( source)
- trim: documentation ( source)
<?php
$strs = ["NLNL LL","LNLN LL LL","NNLL LL LL LL","LNLN LLL LL","LLNN LL LLL"];
foreach ($strs as $s) {
echo "----------------------------\n$s\n";
if (preg_match('~(?:\s[A-Z]{2})+$~', $s, $match)) {
print_r(explode(" ",trim($match[0])));
}
if (preg_match_all('~(?:\G(?!\A)|(?=(?:\s[A-Z]{2})+$))\s\K[A-Z]{2}~', $s, $matches)) {
print_r($matches[0]);
}
}