Convertire i numeri in Numeri Romani!

I numeri romani possono sembrare molto eleganti in una pagina. Questa funzione converte i numeri normali in numeri romani.

  1.  
  2. < ?php
  3.  
  4. /**
  5.  *
  6.  * @create a roman numeral from a number
  7.  *
  8.  * @param int $num
  9.  *
  10.  * @return string
  11.  *
  12.  */
  13. function romanNumerals($num)
  14. {
  15. $n = intval($num);
  16. $res = '';
  17.  
  18. /*** roman_numerals array ***/
  19. $roman_numerals = array(
  20. 'M' => 1000,
  21. 'CM' => 900,
  22. 'D' => 500,
  23. 'CD' => 400,
  24. 'C' => 100,
  25. 'XC' => 90,
  26. 'L' => 50,
  27. 'XL' => 40,
  28. 'X' => 10,
  29. 'IX' => 9,
  30. 'V' => 5,
  31. 'IV' => 4,
  32. 'I' => 1);
  33.  
  34. foreach ($roman_numerals as $roman => $number)
  35. {
  36. /*** divide to get matches ***/
  37. $matches = intval($n / $number);
  38.  
  39. /*** assign the roman char * $matches ***/
  40. $res .= str_repeat($roman, $matches);
  41.  
  42. /*** substract from the number ***/
  43. $n = $n % $number;
  44. }
  45.  
  46. /*** return the res ***/
  47. return $res;
  48. }
  49.  
  50. //ESEMPIO APPLICATO
  51.  
  52. /*** use the current year as the number to convert ***/
  53. $year = date('Y');
  54.  
  55. /*** echo the roman numeral for the year ***/
  56. echo romanNumerals($year);
  57. ?>
  58.