You might want to still use PHP's NumberFormatter
and set the PARSE_INT_ONLY
attribute. Here's its manual: https://www.php.net/manual/en/class.numberformatter.php. You can try something like this:
function romanToInt(string $roman) {
static $nf = null;
if ($nf === null) {
$nf = new \NumberFormatter('@numbers=roman', \NumberFormatter::DECIMAL);
$nf->setAttribute(\NumberFormatter::PARSE_INT_ONLY, true);
}
return $nf->parse($roman);
}