Project

General

Profile

Download (5.16 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * String functions.
5
 */
6

    
7
/**
8
 * Truncates a $string to the specified $length.
9
 *
10
 * If the supplied string is equal to or shorter than the $length, the original
11
 * is returned. If an $appendix is defined, the resulting string will have the
12
 * specified $length including the $appendix.
13
 *
14
 * @param string $string
15
 *   The string to truncate.
16
 * @param int $length
17
 *   The maximun length.
18
 * @param string $appendix
19
 *   An optional appendix.
20
 *
21
 * @return string
22
 *   The truncated string.
23
 */
24
function str_trunk(&$string, $length, $appendix = '') {
25
  if (strlen($string) >= $length) {
26
    return substr($string, 0, $length - strlen($appendix)) . $appendix;
27
  }
28
  else {
29
    return $string;
30
  }
31
}
32

    
33
/**
34
 * Checks if a string starts with some substring.
35
 *
36
 * @param string $str
37
 *   The string to test.
38
 * @param string $sub
39
 *   The substring that the string should start with.
40
 *
41
 * @return bool
42
 *   TRUE if the string starts with the substring.
43
 */
44
function str_beginsWith($str, $sub) {
45
  return (substr($str, 0, strlen($sub)) === $sub);
46
}
47

    
48
/**
49
 * Checks if a string ends with some substring.
50
 *
51
 * @param string $str
52
 *   The string to test.
53
 * @param string $sub
54
 *   The substring that the string should end with.
55
 *
56
 * @return bool
57
 *   TRUE if the string ends with the substring.
58
 */
59
function str_endsWith($str, $sub) {
60
  return (substr($str, strlen($str) - strlen($sub)) === $sub);
61
}
62

    
63
/**
64
 * Replaces all keys in an array with the given values.
65
 *
66
 * All occurences in $array of the key defined in $replace_map are replaced
67
 * with the according values in the $replace_map.
68
 *
69
 * @param array $array
70
 *   The array to modify.
71
 * @param array $replace_map
72
 *   The values to replace.
73
 *
74
 * @return array
75
 *   The modified array.
76
 */
77
function array_replace_key($array, $replace_map) {
78
  foreach ($replace_map as $key => $newkey) {
79
    if (isset($array[$key])) {
80
      $array[$newkey] = $array[$key];
81
      unset($array[$key]);
82
    }
83
  }
84
  return $array;
85
}
86

    
87
/**
88
 * Replaces spaces in a string to underscores and returns the string lowercase.
89
 *
90
 * All occurrences of space characters are replaced with an underscore.
91
 * The string is also transformed to lowercase.
92
 *
93
 * @param string $string
94
 *   The string to modify.
95
 *
96
 * @return string
97
 *   The transformed string.
98
 */
99
function generalizeString($string) {
100
  return str_replace(' ', '_', strtolower($string));
101
}
102

    
103

    
104
function roman_numerals($input_arabic_numeral='') {
105

    
106
  if ($input_arabic_numeral == '') { $input_arabic_numeral = date("Y"); } // DEFAULT OUTPUT: THIS YEAR
107
  $arabic_numeral            = intval($input_arabic_numeral);
108
  $arabic_numeral_text    = "$arabic_numeral";
109
  $arabic_numeral_length    = strlen($arabic_numeral_text);
110

    
111
  if (!preg_match('/[0-9]/', $arabic_numeral_text)) {
112
    return false; }
113

    
114
  if ($arabic_numeral > 4999) {
115
    return false; }
116

    
117
  if ($arabic_numeral < 1) {
118
    return false; }
119

    
120
  if ($arabic_numeral_length > 4) {
121
    return false; }
122

    
123
  $roman_numeral_units    = $roman_numeral_tens        = $roman_numeral_hundreds        = $roman_numeral_thousands        = array();
124
  $roman_numeral_units[0]    = $roman_numeral_tens[0]    = $roman_numeral_hundreds[0]    = $roman_numeral_thousands[0]    = ''; // NO ZEROS IN ROMAN NUMERALS
125

    
126
  $roman_numeral_units[1]='I';
127
  $roman_numeral_units[2]='II';
128
  $roman_numeral_units[3]='III';
129
  $roman_numeral_units[4]='IV';
130
  $roman_numeral_units[5]='V';
131
  $roman_numeral_units[6]='VI';
132
  $roman_numeral_units[7]='VII';
133
  $roman_numeral_units[8]='VIII';
134
  $roman_numeral_units[9]='IX';
135

    
136
  $roman_numeral_tens[1]='X';
137
  $roman_numeral_tens[2]='XX';
138
  $roman_numeral_tens[3]='XXX';
139
  $roman_numeral_tens[4]='XL';
140
  $roman_numeral_tens[5]='L';
141
  $roman_numeral_tens[6]='LX';
142
  $roman_numeral_tens[7]='LXX';
143
  $roman_numeral_tens[8]='LXXX';
144
  $roman_numeral_tens[9]='XC';
145

    
146
  $roman_numeral_hundreds[1]='C';
147
  $roman_numeral_hundreds[2]='CC';
148
  $roman_numeral_hundreds[3]='CCC';
149
  $roman_numeral_hundreds[4]='CD';
150
  $roman_numeral_hundreds[5]='D';
151
  $roman_numeral_hundreds[6]='DC';
152
  $roman_numeral_hundreds[7]='DCC';
153
  $roman_numeral_hundreds[8]='DCCC';
154
  $roman_numeral_hundreds[9]='CM';
155

    
156
  $roman_numeral_thousands[1]='M';
157
  $roman_numeral_thousands[2]='MM';
158
  $roman_numeral_thousands[3]='MMM';
159
  $roman_numeral_thousands[4]='MMMM';
160

    
161
  if ($arabic_numeral_length == 3) { $arabic_numeral_text = "0" . $arabic_numeral_text; }
162
  if ($arabic_numeral_length == 2) { $arabic_numeral_text = "00" . $arabic_numeral_text; }
163
  if ($arabic_numeral_length == 1) { $arabic_numeral_text = "000" . $arabic_numeral_text; }
164

    
165
  $anu = substr($arabic_numeral_text, 3, 1);
166
  $anx = substr($arabic_numeral_text, 2, 1);
167
  $anc = substr($arabic_numeral_text, 1, 1);
168
  $anm = substr($arabic_numeral_text, 0, 1);
169

    
170
  $roman_numeral_text = $roman_numeral_thousands[$anm] . $roman_numeral_hundreds[$anc] . $roman_numeral_tens[$anx] . $roman_numeral_units[$anu];
171
  return ($roman_numeral_text);
172
}
173

    
174
/**
175
 * Converts an integer into the alphabet base (A-Z).
176
 *
177
 * @param int $n This is the number to convert.
178
 * @return string The converted number.
179
 * @author Theriault
180
 *
181
 */
182
function num2alpha($n) {
183
  $r = '';
184
  for ($i = 1; $n >= 0 && $i < 10; $i++) {
185
    $r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
186
    $n -= pow(26, $i);
187
  }
188
  return $r;
189
}
190
?>
(7-7/10)