Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active August 23, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfontjr/6db1779261a0eb7ce6eebe868a59e451 to your computer and use it in GitHub Desktop.
Save vfontjr/6db1779261a0eb7ce6eebe868a59e451 to your computer and use it in GitHub Desktop.
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