Skip to content

Instantly share code, notes, and snippets.

@chill117
Created July 11, 2013 00:31
Show Gist options
  • Save chill117/5971495 to your computer and use it in GitHub Desktop.
Save chill117/5971495 to your computer and use it in GitHub Desktop.
Converts an Arabic Numeral to Roman Numeral. Works for 1 through 4999.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
|----------------------------------------------------------------
| Description
|----------------------------------------------------------------
Converts an Arabic Numeral to Roman Numeral.
Works for 1 through 4999.
|----------------------------------------------------------------
| Dependencies
|----------------------------------------------------------------
CodeIgniter
|----------------------------------------------------------------
| Example Usage
|----------------------------------------------------------------
<?php
echo roman_numerals(14);
?>
Should print the following:
XIV
*/
function roman_numerals($input_arabic_numeral)
{
if (empty($input_arabic_numeral))
return false;
$arabic_numeral = (int) $input_arabic_numeral;
$arabic_numeral_text = (string) $arabic_numeral;
$arabic_numeral_length = strlen($arabic_numeral_text);
if (preg_match('~[0-9]~', $arabic_numeral_text) !== 1)
return false;
if ($arabic_numeral_length > 4)
return false;
if ($arabic_numeral > 4999)
return false;
if ($arabic_numeral < 1)
return false;
$roman_numeral_units = array();
$roman_numeral_tens = array();
$roman_numeral_hundreds = array();
$roman_numeral_thousands = array();
/*
No zeroes in Roman Numerals.
*/
$roman_numeral_units[0] = '';
$roman_numeral_tens[0] = '';
$roman_numeral_hundreds[0] = '';
$roman_numeral_thousands[0] = '';
$roman_numeral_units[1]='I';
$roman_numeral_units[2]='II';
$roman_numeral_units[3]='III';
$roman_numeral_units[4]='IV';
$roman_numeral_units[5]='V';
$roman_numeral_units[6]='VI';
$roman_numeral_units[7]='VII';
$roman_numeral_units[8]='VIII';
$roman_numeral_units[9]='IX';
$roman_numeral_tens[1]='X';
$roman_numeral_tens[2]='XX';
$roman_numeral_tens[3]='XXX';
$roman_numeral_tens[4]='XL';
$roman_numeral_tens[5]='L';
$roman_numeral_tens[6]='LX';
$roman_numeral_tens[7]='LXX';
$roman_numeral_tens[8]='LXXX';
$roman_numeral_tens[9]='XC';
$roman_numeral_hundreds[1]='C';
$roman_numeral_hundreds[2]='CC';
$roman_numeral_hundreds[3]='CCC';
$roman_numeral_hundreds[4]='CD';
$roman_numeral_hundreds[5]='D';
$roman_numeral_hundreds[6]='DC';
$roman_numeral_hundreds[7]='DCC';
$roman_numeral_hundreds[8]='DCCC';
$roman_numeral_hundreds[9]='CM';
$roman_numeral_thousands[1]='M';
$roman_numeral_thousands[2]='MM';
$roman_numeral_thousands[3]='MMM';
$roman_numeral_thousands[4]='MMMM';
if ($arabic_numeral_length == 3) { $arabic_numeral_text = "0" . $arabic_numeral_text; }
if ($arabic_numeral_length == 2) { $arabic_numeral_text = "00" . $arabic_numeral_text; }
if ($arabic_numeral_length == 1) { $arabic_numeral_text = "000" . $arabic_numeral_text; }
$anu = substr($arabic_numeral_text, 3, 1);
$anx = substr($arabic_numeral_text, 2, 1);
$anc = substr($arabic_numeral_text, 1, 1);
$anm = substr($arabic_numeral_text, 0, 1);
$roman_numeral_text = $roman_numeral_thousands[$anm];
$roman_numeral_text .= $roman_numeral_hundreds[$anc];
$roman_numeral_text .= $roman_numeral_tens[$anx];
$roman_numeral_text .= $roman_numeral_units[$anu];
return $roman_numeral_text;
}
/* End of file roman_numerals_helper.php */
/* Location: ./application/helpers/roman_numerals_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment