Roman Conversion

<?php

$lines = file('http://hyperspatial.com/romans.txt');
//$lines = array('MM','IX','X');
$array_of_converted_values = array();

foreach($lines as $line_num => $line) $array_of_converted_values[] = convert_roman_numeral(htmlspecialchars($line));

print_r($array_of_converted_values);

function convert_roman_numeral($roman_numeral = 'M'){
$roman_array = 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);
$result = 0;
foreach($roman_array as $key => $value) {
while(strpos($roman_numeral, $key) === 0) {
$result += $value;
$roman_numeral = substr($roman_numeral, strlen($key));
}
}
return $result;
}
?>

0 Comments

You must be logged in to post a comment.