Skip to content

Instantly share code, notes, and snippets.

@cgray
Last active July 19, 2018 07:55
Show Gist options
  • Save cgray/5633077 to your computer and use it in GitHub Desktop.
Save cgray/5633077 to your computer and use it in GitHub Desktop.
[PHP] Convert Roman Numerals to Decimal
// from http://stackoverflow.com/questions/6265596/how-to-convert-a-roman-numeral-to-integer-in-php
$romans = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1,
);
$roman = 'MMMCMXCIX';
$result = 0;
foreach ($romans as $key => $value) {
while (strpos($roman, $key) === 0) {
$result += $value;
$roman = substr($roman, strlen($key));
}
}
echo $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment