Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active Aug 23, 2019
Embed
What would you like to do?
Convert Integers to Roman Numerals
<? php
add_shortcode( 'roman_copyright', 'roman_copyright_shortcode' );
/* to use the shortcode, insert this into the footer
[roman_copyright before="Copyright " first="2003" after=" Victor M. Font Jr."]
*/
<?php
function roman_numerals($integer) {
// Convert the integer into an integer (just to make sure)
$integer = intval($integer);
$result = '';
// Create a lookup array that contains all of the Roman numerals.
$lookup = 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($lookup as $roman => $value){
// Determine the number of matches
$matches = intval($integer/$value);
// Add the same number of characters to the string
$result .= str_repeat($roman,$matches);
// Set the integer to be the remainder of the integer and the value
$integer = $integer % $value;
}
// The Roman numeral should be built, return it
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment