-
Notifications
You must be signed in to change notification settings - Fork 1
/
customFunctions.php
58 lines (47 loc) · 1.2 KB
/
customFunctions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Converts integer to Roman.
*
* @param int $integer
* Integer to be converted to Roman string.
*
* @return string
*/
function integerToRoman($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 = [
'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;
}
function stringToNumber($string)
{
$int = (int)filter_var($string, FILTER_SANITIZE_NUMBER_INT);
return ($int == 0) ? ' ' : $int;
}
?>