1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace cse\helpers; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Email |
9
|
|
|
* |
10
|
|
|
* @package cse\helpers |
11
|
|
|
*/ |
12
|
|
|
class Email |
13
|
|
|
{ |
14
|
|
|
const UTF8 = 'UTF-8'; |
15
|
|
|
const PATTERN = '(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Hide email |
19
|
|
|
* |
20
|
|
|
* @param $email |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public static function hide(string $email): string |
25
|
|
|
{ |
26
|
|
|
list($name, $domain) = explode('@', $email); |
27
|
|
|
|
28
|
|
|
$length = mb_strlen($name, self::UTF8); |
29
|
|
|
|
30
|
|
|
if ($length <= 4) { |
31
|
|
|
// abcd => a*** |
32
|
|
|
$result = mb_substr($name, 0, 1, self::UTF8) . str_repeat('*', $length - 1); |
33
|
|
|
} elseif ($length > 4 && $length <= 6) { |
34
|
|
|
// abcdef => a****f |
35
|
|
|
$result = mb_substr($name, 0, 1, self::UTF8) |
36
|
|
|
. str_repeat('*', $length - 2) |
37
|
|
|
. mb_substr($name, -1, 1, self::UTF8); |
38
|
|
|
} else { |
39
|
|
|
// abcdefghijk => a****f****k |
40
|
|
|
// abcdefghijkl => a****f*****k |
41
|
|
|
$result = mb_substr($name, 0, 1, self::UTF8) |
42
|
|
|
. str_repeat('*', (int) floor(($length - 3) / 2)) |
43
|
|
|
. mb_substr($name, (int) ceil(($length - 2) / 2), 1, self::UTF8) |
44
|
|
|
. str_repeat('*', (int) ceil(($length - 3) / 2)) |
45
|
|
|
. mb_substr($name, -1, 1, self::UTF8); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $result . '@' . $domain; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check domain to email |
53
|
|
|
* |
54
|
|
|
* @param string $email |
55
|
|
|
* @param string $domain |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public static function checkDomain(string $email, string $domain): bool |
60
|
|
|
{ |
61
|
|
|
$email = trim($email); |
62
|
|
|
|
63
|
|
|
return strlen($email) == 0 ? false : (preg_match('/^.*@' . $domain . '$/i', $email) === 1); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check email |
68
|
|
|
* |
69
|
|
|
* @param string $email |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public static function is(string $email): bool |
74
|
|
|
{ |
75
|
|
|
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check email exist to string |
80
|
|
|
* |
81
|
|
|
* @param string $string |
82
|
|
|
* @param string $pattern |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public static function exist(string $string, string $pattern = self::PATTERN): bool |
87
|
|
|
{ |
88
|
|
|
return preg_match('/' . $pattern . '/iD', $string) === 1; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Extract email to string |
93
|
|
|
* |
94
|
|
|
* @param string $string |
95
|
|
|
* @param string $pattern |
96
|
|
|
* |
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
|
|
public static function extract(string $string, string $pattern = self::PATTERN): ?string |
100
|
|
|
{ |
101
|
|
|
return preg_match('/' . $pattern . '/iD', $string, $email) === 1 ? $email[0] : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Extract all email to string |
106
|
|
|
* |
107
|
|
|
* @param string $string |
108
|
|
|
* @param string $pattern |
109
|
|
|
* |
110
|
|
|
* @return null|array |
111
|
|
|
*/ |
112
|
|
|
public static function extractAll(string $string, string $pattern = self::PATTERN): ?array |
113
|
|
|
{ |
114
|
|
|
preg_match_all('/' . $pattern . '/i', $string, $email); |
115
|
|
|
|
116
|
|
|
return empty($email[0]) ? null : $email[0]; |
117
|
|
|
} |
118
|
|
|
} |