-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelper.php
115 lines (107 loc) · 2.84 KB
/
Helper.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
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
115
<?php
/**
* Useful functions
*
* @file Helper.php
*
* PHP version 8.0+
*
* @author Alexander Yancharuk <alex at itvault dot info>
* @copyright © 2012-2021 Alexander Yancharuk
* @date Сбт Апр 21 20:59:37 2012
* @license The BSD 3-Clause License
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
*/
namespace Veles;
/**
* Useful functions
*
* @author Alexander Yancharuk <alex at itvault dot info>
*/
class Helper
{
/**
* Generate random symbol sequence given length
*
* By default setting is for Blowfish salt generate
*
* @param int $length String length
* @param string $letters Group of symbols
*
* @return string
*/
public static function genStr(
$length = 22,
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./'
) {
return substr(str_shuffle(str_repeat($letters, 5)), 0, $length);
}
/**
* Email check
*
* @param string $email
* @return bool
*/
public static function validateEmail($email)
{
return (bool) preg_match(
'/^(?:[a-zA-Z0-9]|_|\-|\.)+@(?:(?:[a-z0-9_]|\-)+\.)+[a-z]{2,6}$/',
$email
);
}
/**
* Check email-domain
*
* @param string $email
* @return bool
*/
public static function checkEmailDomain($email)
{
list(, $domain) = explode('@', $email);
return checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A');
}
/**
* Translate cyrillic into translit by GOST 7.79-2000 standard
*
* @param string $string
* @return string
*/
public static function translit($string)
{
$symbols = [
'А'=>'A','Б'=>'B','В'=>'V','Г'=>'G',
'Д'=>'D','Е'=>'E','Ё'=>'YO','Ж'=>'ZH','З'=>'Z','И'=>'I',
'Й'=>'J','К'=>'K','Л'=>'L','М'=>'M','Н'=>'N',
'О'=>'O','П'=>'P','Р'=>'R','С'=>'S','Т'=>'T',
'У'=>'U','Ф'=>'F','Х'=>'H','Ц'=>'CZ','Ч'=>'CH',
'Ш'=>'SH','Щ'=>'SHH','Ъ'=>'','Ы'=>'Y','Ь'=>'',
'Э'=>'E','Ю'=>'YU','Я'=>'YA','а'=>'a','б'=>'b',
'в'=>'v','г'=>'g','д'=>'d','е'=>'e','ё'=>'yo','ж'=>'zh',
'з'=>'z','и'=>'i','й'=>'j','к'=>'k','л'=>'l',
'м'=>'m','н'=>'n','о'=>'o','п'=>'p','р'=>'r',
'с'=>'s','т'=>'t','у'=>'u','ф'=>'f','х'=>'h',
'ц'=>'ts','ч'=>'ch','ш'=>'sh','щ'=>'shh','ъ'=>'',
'ы'=>'y','ь'=>'','э'=>'e','ю'=>'yu','я'=>'ya',
' '=>'-','"'=>'','.'=>'',','=>'','!'=>'','?'=>'',
'('=>'',')'=>'','#'=>'','@'=>'','*'=>'','&'=>'',
'['=>'',']'=>'',':'=>'',';'=>'','<'=>'','>'=>'',
'+'=>''
];
return strtr(mb_strtolower($string, 'UTF-8'), $symbols);
}
/**
* Method for alias generation
*
* @param string $url URL для алиаса
*
* @return string
*/
public static function makeAlias($url)
{
$alias = htmlspecialchars_decode($url);
$alias = preg_replace('/[^a-z^а-яё^\d^ ^-]/iu', '', $alias);
$alias = Helper::translit($alias);
$alias = preg_replace('/\-+/', '-', $alias);
return $alias;
}
}