PHP and Roman Numerals

After hunting around for functions that convert Arabic numbers to Roman numerals and vice versa, everything I found was huge (~100 lines) except for one very graceful function to convert Arabic numbers to Roman numerals. So I combined it with my own Roman to Arabic converter.

Pass it an int, a numeric string or a string of Roman numerals, and it will convert from one to the other.

Feel free to use the function in your own projects, but please keep the attributions intact.

function roman($num){
		'M' =>1000,
		'D' =>500,
		'C' =>100,
		'L' =>50,
		'X' =>10,
		'V' =>5,
		'I' =>1
		// To Roman:
		foreach($numerals as $roman=>$value){
		// To Arabic:
	return $result;

// Usage
echo roman('1000'); // M
echo roman(1100);   // MC
echo roman('MC');   // 1100
Filed under: PHP Comments Off