Last active
August 23, 2019 13:53
-
-
Save vfontjr/6db1779261a0eb7ce6eebe868a59e451 to your computer and use it in GitHub Desktop.
Convert Integers to Roman Numerals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? 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."] | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function roman_copyright_shortcode( $atts ) { | |
$defaults = [ | |
'after' => '', | |
'before' => '', | |
'copyright' => '©', | |
'first' => '', | |
]; | |
$atts = shortcode_atts( $defaults, $atts, 'roman_copyright' ); | |
$output = $atts['before'] . $atts['copyright'] . ' '; | |
if ( '' !== $atts['first'] && date( 'Y' ) !== $atts['first'] ) { | |
$output .= roman_numerals( $atts['first'] ) . '–'; | |
} | |
$output .= roman_numerals( date( 'Y' ) ) . $atts['after']; | |
return apply_filters( 'roman_copyright_shortcode', $output, $atts ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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