Two functions to convert integers into a roman string and back. Don't know if anybody needs this, but it was fun to make.
By : Dios
<?php // Function that calculates the roman string to the given number: function dec2roman($f) { // Return false if either $f is not a real number, $f is bigger than 3999 or $f is lower or equal to 0: if(!is_numeric($f) || $f > 3999 || $f <= 0) return false;
// Replace some spacial cases and return the string: return str_replace(array('VIV','LXL','DCD'), array('IX','XC','CM'), $return); }
// Function to get the decimal value of a roman string: function roman2dec($str = '') { // Return false if not at least one letter is in the string: if(is_numeric($str)) return false;
// Convert the string to an array of roman values: for($i = 0; $i < strlen($str); $i++) if(isset($roman[strtoupper($str[$i])])) $values[] = $roman[strtoupper($str[$i])];
// Calculate the sum of that array: $sum = 0; while($current = current($values)) { $next = next($values); $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current; }
// Return the value: return $sum; } ?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.