1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Strings.php |
4
|
|
|
*/ |
5
|
|
|
namespace w3l\Holt45; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Handle strings(convert, encode, replace, etc). |
9
|
|
|
*/ |
10
|
|
|
trait Strings |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Obfuscate string (url-safe and somewhat hard to guess). |
14
|
|
|
* |
15
|
|
|
* @param string $input The text that should be obfuscated |
16
|
|
|
* @return string Obfuscated string |
17
|
|
|
*/ |
18
|
|
|
public static function obfuscateString($input) |
19
|
|
|
{ |
20
|
|
|
return bin2hex(base64_encode(strrev($input))); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Deobfuscate string |
25
|
|
|
* |
26
|
|
|
* @param string $input Obfuscated string |
27
|
|
|
* @return string Deobfuscated string |
28
|
|
|
*/ |
29
|
|
|
public static function deobfuscateString($input) |
30
|
|
|
{ |
31
|
|
|
return strrev(base64_decode(hex2bin($input))); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Convert <textarea> to [textarea]. |
36
|
|
|
* |
37
|
|
|
* @param string $html |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public static function textareaEncode($html) |
41
|
|
|
{ |
42
|
|
|
return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Convert [textarea] to <textarea>. |
47
|
|
|
* |
48
|
|
|
* @param string $html |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public static function textareaDecode($html) |
52
|
|
|
{ |
53
|
|
|
return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* To replace "Hallo [@var] world" with $value. |
58
|
|
|
* |
59
|
|
|
* Example: |
60
|
|
|
* ```php |
61
|
|
|
* replace_string($string, array("val1" => "foo", "val2" => "bar")) |
62
|
|
|
* ``` |
63
|
|
|
* |
64
|
|
|
* @param string $langString String containing placeholder. |
65
|
|
|
* @param array $dynamicContent key->value array. |
66
|
|
|
* @return string String with placeholder replaced. |
67
|
|
|
*/ |
68
|
|
|
public static function replaceString($langString, $dynamicContent = array()) |
69
|
|
|
{ |
70
|
|
|
foreach ($dynamicContent as $k => $v) { |
71
|
|
|
$langString = str_replace("[@".$k."]", $v, $langString); |
72
|
|
|
} |
73
|
|
|
return $langString; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates rainbow-colored text. |
78
|
|
|
* |
79
|
|
|
* @uses Holt45::colorBlend() |
80
|
|
|
* @uses Holt45::rgbhex() |
81
|
|
|
* |
82
|
|
|
* @param string $text Text wanted coloured. |
83
|
|
|
* @return string String with span-tags with color. |
84
|
|
|
*/ |
85
|
|
|
public static function rainbowText($text) |
86
|
|
|
{ |
87
|
|
|
$colorsBase = array( |
88
|
|
|
array(255, 0, 0), |
89
|
|
|
array(255, 102, 0), |
90
|
|
|
array(255, 238, 0), |
91
|
|
|
array(0, 255, 0), |
92
|
|
|
array(0, 153, 255), |
93
|
|
|
array(68, 0, 255), |
94
|
|
|
array(153, 0, 255) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$colorsBuild = array(); |
98
|
|
|
|
99
|
|
|
$strlenText = strlen($text); |
100
|
|
|
|
101
|
|
|
if ($strlenText > 7) { |
102
|
|
|
while (count($colorsBuild) < $strlenText) { |
103
|
|
|
for ($i = 0, $size = count($colorsBase); $i < $size; $i++) { |
104
|
|
|
|
105
|
|
|
$colorsBuild[] = $colorsBase[$i]; |
106
|
|
|
|
107
|
|
|
if (count($colorsBuild) >= $strlenText) { |
108
|
|
|
continue 2; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($i < count($colorsBase)-1) { |
112
|
|
|
|
113
|
|
|
$colorsBuild[] = self::colorBlend($colorsBase[$i], $colorsBase[$i+1]); |
114
|
|
|
|
115
|
|
|
if (count($colorsBuild) >= $strlenText) { |
116
|
|
|
continue 2; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$colorsBase = $colorsBuild; |
121
|
|
|
$colorsBuild = array(); |
122
|
|
|
} |
123
|
|
|
} elseif ($strlenText <= 7) { |
124
|
|
|
$colorsBuild = $colorsBase; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$arrayText = str_split($text); |
128
|
|
|
$returnText = ""; |
129
|
|
|
for ($i = 0, $size = count($arrayText); $i < $size; $i++) { |
130
|
|
|
$returnText .= '<span style="color: #'.self::rgbhex($colorsBuild[$i]).';">'.$arrayText[$i].'</span>'; |
131
|
|
|
} |
132
|
|
|
return $returnText; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the symbol from a list of keyboard-keys... |
137
|
|
|
* |
138
|
|
|
* @used-by Holt45::kbdShortcut() |
139
|
|
|
* |
140
|
|
|
* @param string $inputKey Text |
141
|
|
|
* @param string $inputOperatingSystem default|auto|win|mac|linux |
142
|
|
|
* @return null|string HTML Entity (decimal) |
143
|
|
|
*/ |
144
|
|
|
public static function kbdSymbol($inputKey, $inputOperatingSystem = "default") |
145
|
|
|
{ |
146
|
|
|
$inputKey = mb_strtolower($inputKey); |
147
|
|
|
|
148
|
|
|
if ($inputOperatingSystem == "auto") { |
149
|
|
|
|
150
|
|
|
$inputOperatingSystem = "default"; |
151
|
|
|
|
152
|
|
|
$getClientOperatingSystem = self::getClientOperatingSystem(); |
|
|
|
|
153
|
|
|
|
154
|
|
|
if ($getClientOperatingSystem == "linux" || |
155
|
|
|
$getClientOperatingSystem == "mac" || |
156
|
|
|
$getClientOperatingSystem == "windows") { |
157
|
|
|
$inputOperatingSystem = $getClientOperatingSystem; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$arrayConvert = array( |
162
|
|
|
"return" => "enter", |
163
|
|
|
"control" => "ctrl", |
164
|
|
|
"escape" => "esc", |
165
|
|
|
"caps lock" => "caps-lock", |
166
|
|
|
"page up" => "page-up", |
167
|
|
|
"page down" => "page-down", |
168
|
|
|
"arrow left" => "arrow-left", |
169
|
|
|
"left" => "arrow-left", |
170
|
|
|
"arrow up" => "arrow-up", |
171
|
|
|
"up" => "arrow-up", |
172
|
|
|
"arrow right" => "arrow-right", |
173
|
|
|
"right" => "arrow-right", |
174
|
|
|
"arrow down" => "arrow-down", |
175
|
|
|
"down" => "arrow-down" |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
/* Convert input */ |
179
|
|
|
if (array_key_exists($inputKey, $arrayConvert)) { |
180
|
|
|
$inputKey = $arrayConvert[$inputKey]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$arrayKeySymbols = array( |
184
|
|
|
"shift" => array("default" => "⇧"), |
185
|
|
|
"opt" => array("default" => "⌥"), |
186
|
|
|
"enter" => array("default" => "⏎", "mac" => "⌤"), |
187
|
|
|
"alt" => array("default" => "⎇", "mac" => "⌥"), |
188
|
|
|
"delete" => array("default" => "⌫"), |
189
|
|
|
"ctrl" => array("default" => "✲", "windows" => "✲", "linux" => "⎈", "mac" => "^"), |
190
|
|
|
"esc" => array("default" => "⎋"), |
191
|
|
|
"command" => array("default" => "⌘"), |
192
|
|
|
"tab" => array("default" => "↹", "mac" => "⇥"), |
193
|
|
|
"caps-lock" => array("default" => "A", "mac" => "⇪"), |
194
|
|
|
"page-up" => array("default" => "▲", "mac" => "⇞"), |
195
|
|
|
"page-down" => array("default" => "▼", "mac" => "⇟"), |
196
|
|
|
"arrow-left" => array("default" => "←"), |
197
|
|
|
"arrow-up" => array("default" => "↑"), |
198
|
|
|
"arrow-right" => array("default" => "→"), |
199
|
|
|
"arrow-down" => array("default" => "↓"), |
200
|
|
|
// Sun |
201
|
|
|
"compose" => array("default" => "⎄"), |
202
|
|
|
"meta" => array("default" => "◆") |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
if (array_key_exists($inputKey, $arrayKeySymbols)) { |
206
|
|
|
|
207
|
|
|
return ((array_key_exists($inputOperatingSystem, $arrayKeySymbols[$inputKey])) ? |
208
|
|
|
$arrayKeySymbols[$inputKey][$inputOperatingSystem] : |
209
|
|
|
$arrayKeySymbols[$inputKey]["default"]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return null; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Show fancy buttons for keyboard-shortcuts. |
217
|
|
|
* |
218
|
|
|
* @uses Holt45::kbdSymbol() |
219
|
|
|
* |
220
|
|
|
* @param array $inputArrayKeys |
221
|
|
|
* @param string $inputOperatingSystem |
222
|
|
|
* @param string $inputKbdClass |
223
|
|
|
* @param string $inputKbdSymbolClass |
224
|
|
|
* @param string $inputJoinGlue Glue |
225
|
|
|
* @return string String of html |
226
|
|
|
*/ |
227
|
|
|
public static function kbdShortcut( |
228
|
|
|
$inputArrayKeys, |
229
|
|
|
$inputOperatingSystem = "default", |
230
|
|
|
$inputKbdClass = "holt45-kbd", |
231
|
|
|
$inputKbdSymbolClass = "holt45-kbd__symbol", |
232
|
|
|
$inputJoinGlue = " + " |
233
|
|
|
) { |
234
|
|
|
$returnArray = array(); |
235
|
|
|
|
236
|
|
|
foreach ($inputArrayKeys as $key) { |
237
|
|
|
|
238
|
|
|
$kbdSymbol = self::kbdSymbol($key, $inputOperatingSystem); |
239
|
|
|
|
240
|
|
|
$kbdSymbolHtml = ""; |
241
|
|
|
|
242
|
|
|
if ($kbdSymbol !== null) { |
243
|
|
|
$kbdSymbolHtml = '<span class="'.$inputKbdSymbolClass.'">'.$kbdSymbol.'</span>'; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$returnArray[] = '<kbd class="'.$inputKbdClass.'">'.$kbdSymbolHtml.$key.'</kbd>'; |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return implode($inputJoinGlue, $returnArray); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Attempting to keep CSS on one line with scoped style. |
255
|
|
|
* |
256
|
|
|
* NOTICE: This is a rough estimate, font type, design, etc affects the results. |
257
|
|
|
* |
258
|
|
|
* @param string $text |
259
|
|
|
* @param string $cssSelector |
260
|
|
|
* @param int $fontSizePx |
261
|
|
|
* @param int $minPageWidthPx |
262
|
|
|
* @return null|string Scoped style |
263
|
|
|
*/ |
264
|
|
|
public static function cssOneLineText($text, $cssSelector = "h1", $fontSizePx = 36, $minPageWidthPx = 320) |
265
|
|
|
{ |
266
|
|
|
$countText = strlen($text)+1; |
267
|
|
|
|
268
|
|
|
$fontWidth = ($fontSizePx / 2); |
269
|
|
|
|
270
|
|
|
if ($minPageWidthPx < $countText) { |
271
|
|
|
$minPageWidthPx = $countText; |
272
|
|
|
} |
273
|
|
|
if (($countText * $fontWidth) > $minPageWidthPx) { |
274
|
|
|
|
275
|
|
|
$cssNewFontSizePx = round(($minPageWidthPx / $countText) * 2, 2); |
276
|
|
|
$cssNewFontSizeVw = round((100/$countText) * 2, 2); |
277
|
|
|
|
278
|
|
|
$cssBreakpointPx = round($countText * $fontWidth); |
279
|
|
|
|
280
|
|
|
return '<style scoped>@media (max-width: '.$cssBreakpointPx.'px) { '.$cssSelector.' { font-size: '.$cssNewFontSizePx.'px; font-size: '.$cssNewFontSizeVw.'vw; } }</style>'; |
|
|
|
|
281
|
|
|
} |
282
|
|
|
return null; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.