Misc/Numeralo.php
/**
	* Converts Roman numerals to natural numbers.
	*
	* @static
	* @param string $numerals the Roman numerals to be converted
	* @throws \InvalidArgumentException if the provided $numerals argument contains invalid characters.
	* @return int the equivalent number
	*/
Converts Roman numerals to natural numbers.
@static
@param string $numerals the Roman numerals to be converted
@throws \InvalidArgumentException if the provided $numerals argument contains invalid characters.
@return int the equivalent number
Converts Roman numerals to natural numbers . @static @param string $numerals the Roman numerals to be converted @throws \ InvalidArgumentException if the provided $numerals argument contains invalid characters . @return int the equivalent number
public static function numerals_to_number($numerals) {
		$number = 0;
		$numeral_string = strrev((string) $numerals);
		$length = strlen($numeral_string);
		$prev_number = false;
		$is_accented = false;
		for($i = 0; $i < $length; $i++) {
			$char = $numeral_string[$i];
			if($char == '|') 
			{
				$is_accented = true;
				continue;
			}
			else if($is_accented)
			{
				$char .= '|';
				$is_accented = false;
			}
			if(!isset(self::$NUMERALS_TO_NUMBER[$char]))
				throw new \InvalidArgumentException("Invalid character '{$char}' in numeral string");
			$num = self::$NUMERALS_TO_NUMBER[$char];
			if($prev_number)
			{
				if(($prev_number / 5) == $num || ($prev_number / 10) == $num)
					$number -= $num;
				else
					$number += $num;
			}
			else
				$number += $num;
			$prev_number = $num;
		}
		return $number;
	}
public static function numerals_to_number ( $ numerals ) { $ number = 0 ; $ numeral_string = strrev ( ( string ) $ numerals ) ; $ length = strlen ( $ numeral_string ) ; $ prev_number = false ; $ is_accented = false ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) { $ char = $ numeral_string [ $ i ] ; if ( $ char == '|' ) { $ is_accented = true ; continue ; } else if ( $ is_accented ) { $ char .= '|' ; $ is_accented = false ; } if ( ! isset ( self :: $ NUMERALS_TO_NUMBER [ $ char ] ) ) throw new \ InvalidArgumentException ( " Invalid character ' { $ char } ' in numeral string " ) ; $ num = self :: $ NUMERALS_TO_NUMBER [ $ char ] ; if ( $ prev_number ) { if ( ( $ prev_number / 5 ) == $ num || ( $ prev_number / 10 ) == $ num ) $ number -= $ num ; else $ number += $ num ; } else $ number += $ num ; $ prev_number = $ num ; } return $ number ; }
Converts Roman numerals to natural numbers.
Converts Roman numerals to natural numbers .
//check if it is an accent character //skip this iteration and process it in the next one as the accent applies to the next char //TODO Make a check using maybe regex at the beginning of the method. //this is where the magic happens //if the previous number divided by 5 or 10 is equal to the current number, then we subtract eg. 9 = IX. I = 1, X = 10, 10/10 = 1