Numbers to Roman Numbers with php
I found this code here: http://php.net/manual/en/function.base-convert.php
Optimized and prettified function:
/** * @param int $number * @return string */function numberToRomanRepresentation($number) { $map = 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); $returnValue = ''; while ($number > 0) { foreach ($map as $roman => $int) { if($number >= $int) { $number -= $int; $returnValue .= $roman; break; } } } return $returnValue;}
Another way to do that
<?php function ConverToRoman($num){ $n = intval($num); $res = ''; //array of roman numbers $romanNumber_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); foreach ($romanNumber_Array as $roman => $number){ //divide to get matches $matches = intval($n / $number); //assign the roman char * $matches $res .= str_repeat($roman, $matches); //substract from the number $n = $n % $number; } // return the result return $res; } echo ConverToRoman(23); ?>
function rome($N){ $c='IVXLCDM'; for($a=5,$b=$s='';$N;$b++,$a^=7) for($o=$N%$a,$N=$N/$a^0;$o--;$s=$c[$o>2?$b+$N-($N&=-2)+$o=1:$b].$s); return $s;}// from polish wiki