-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.php
More file actions
114 lines (98 loc) · 3.09 KB
/
Copy pathStrings.php
File metadata and controls
114 lines (98 loc) · 3.09 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace ADT\Utils;
use Transliterator;
class Strings
{
/**
* Transfer "😊 𝓚𝓪𝓻𝓲𝓷𝓪 𝓒𝓱𝓮𝓫𝓪𝓷 😊" to " Karina Cheban ", but leaves "Tomáš Kudělka" intact
*/
public static function toLatin(string $s): string
{
return preg_replace_callback(
"/[^\p{Common}\p{Latin}]|(?:
\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)/x",
function ($char) {
return \Nette\Utils\Strings::toAscii($char[0]);
},
$s
);
}
/**
* Check if string contains characters with code larger than specified code, for example emoticons
* You can exclude some characters from check and thus allow them, for example €
*/
public static function containsCharactersLargerThen(string $s, int $code, string $exclude = ''): bool
{
foreach (mb_str_split($s) as $c) {
if (mb_strpos($exclude, $c) !== false) {
continue;
}
if (mb_ord($c) > $code) {
return true;
}
}
return false;
}
public static function removeDiacritics(string $s): string
{
$transliterator = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', Transliterator::FORWARD);
return $transliterator->transliterate($s);
}
public static function validateFullName(string $fullName): bool
{
$pattern = [
"/([\s])+(-)/" => "-",
"/(-)([\s])+/" => "-",
"/([\s])+/" => " ",
];
$fullName = trim(preg_replace(array_keys($pattern), array_values($pattern), $fullName));
return (bool) preg_match("/^
(?:
(?=[\-.]*[A-zÀ-ÿěščřžýáíéóúůďťňĎŇŤŠČŘŽÝÁÍÉÚŮĚÓ][\-.]*) (?# Následující slovo obsahuje alespoň jedno písmeno)
(?:[A-zÀ-ÿěščřžýáíéóúůďťňĎŇŤŠČŘŽÝÁÍÉÚŮĚÓ\-.']){2,} (?# Matchnu slovo o min. dvou znacích)
|
- (?# Matchnu pomlčku mezi slovy)
)
(?:
\ *[ ]\ * (?# Oddělovačem slova jsou mezery a .,-')
(?:
(?=[\-.]*[A-zÀ-ÿěščřžýáíéóúůďťňĎŇŤŠČŘŽÝÁÍÉÚŮĚÓ][\-.']*) (?# Následující slovo obsahuje alespoň jedno písmeno)
[A-zÀ-ÿěščřžýáíéóúůďťňĎŇŤŠČŘŽÝÁÍÉÚŮĚÓ\-.,']{2,}
|
- (?# Matchnu pomlčku mezi slovy)
)
)+ (?# Slov může být více, ale min. dvě)
$
/mx", $fullName);
}
public static function convertToType($str)
{
if (!is_string($str)) {
return $str;
}
// Pokud řetězec obsahuje pouze číslice
if (ctype_digit($str)) {
// když začíná 0 a má víc jak 1 znak, vratíme string
if (strpos($str, '0') === 0 && strlen($str) > 1) {
return $str;
}
return (int)$str; // jinak vratíme integer
}
// Pokud řetězec obsahuje číslice a případně jednu desetinnou tečku, vratíme float
if (preg_match('/^\d+\.\d+$/', $str)) {
return (float)$str;
}
// Pro "true" a "false" vrátíme boolean
if (strtolower($str) === 'true') {
return true;
}
if (strtolower($str) === 'false') {
return false;
}
// V opačném případě vratíme původní řetězec
return $str;
}
}