1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\helpers; |
4
|
|
|
|
5
|
|
|
class StringHelper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648) |
9
|
|
|
* |
10
|
|
|
* > Note: Base 64 padding `=` may be at the end of the returned string. |
11
|
|
|
* > `=` is not transparent to URL encoding. |
12
|
|
|
* |
13
|
|
|
* @see https://tools.ietf.org/html/rfc4648#page-7 |
14
|
|
|
* @param string $input the string to encode. |
15
|
|
|
* @return string encoded string. |
16
|
|
|
*/ |
17
|
|
|
public function base64UrlEncode($input): string |
18
|
|
|
{ |
19
|
|
|
return \strtr(\base64_encode($input), '+/', '-_'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648) |
24
|
|
|
* |
25
|
|
|
* @see https://tools.ietf.org/html/rfc4648#page-7 |
26
|
|
|
* @param string $input encoded string. |
27
|
|
|
* @return string decoded string. |
28
|
|
|
*/ |
29
|
|
|
public function base64UrlDecode($input): string |
30
|
|
|
{ |
31
|
|
|
return \base64_decode(\strtr($input, '-_', '+/')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the number of bytes in the given string. |
36
|
|
|
* This method ensures the string is treated as a byte array by using `mb_strlen()`. |
37
|
|
|
* @param string $string the string being measured for length |
38
|
|
|
* @return int the number of bytes in the given string. |
39
|
|
|
*/ |
40
|
|
|
public function byteLength($string): int |
41
|
|
|
{ |
42
|
|
|
return \mb_strlen($string, '8bit'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the portion of string specified by the start and length parameters. |
47
|
|
|
* This method ensures the string is treated as a byte array by using `mb_substr()`. |
48
|
|
|
* @param string $string the input string. Must be one character or longer. |
49
|
|
|
* @param int $start the starting position |
50
|
|
|
* @param int $length the desired portion length. If not specified or `null`, there will be |
51
|
|
|
* no limit on length i.e. the output will be until the end of the string. |
52
|
|
|
* @return string the extracted part of string, or FALSE on failure or an empty string. |
53
|
|
|
* @see http://www.php.net/manual/en/function.substr.php |
54
|
|
|
*/ |
55
|
|
|
public function byteSubstr($string, $start, $length = null): string |
56
|
|
|
{ |
57
|
|
|
return \mb_substr($string, $start, $length ?? \mb_strlen($string, '8bit'), '8bit'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $string |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function rus2translit($string): string |
65
|
|
|
{ |
66
|
|
|
$converter = [ |
67
|
|
|
'а' => 'a', 'б' => 'b', 'в' => 'v', |
68
|
|
|
'г' => 'g', 'д' => 'd', 'е' => 'e', |
69
|
|
|
'ё' => 'e', 'ж' => 'zh', 'з' => 'z', |
70
|
|
|
'и' => 'i', 'й' => 'y', 'к' => 'k', |
71
|
|
|
'л' => 'l', 'м' => 'm', 'н' => 'n', |
72
|
|
|
'о' => 'o', 'п' => 'p', 'р' => 'r', |
73
|
|
|
'с' => 's', 'т' => 't', 'у' => 'u', |
74
|
|
|
'ф' => 'f', 'х' => 'h', 'ц' => 'c', |
75
|
|
|
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', |
76
|
|
|
'ь' => '', 'ы' => 'y', 'ъ' => '', |
77
|
|
|
'э' => 'e', 'ю' => 'yu', 'я' => 'ya', |
78
|
|
|
|
79
|
|
|
'А' => 'A', 'Б' => 'B', 'В' => 'V', |
80
|
|
|
'Г' => 'G', 'Д' => 'D', 'Е' => 'E', |
81
|
|
|
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', |
82
|
|
|
'И' => 'I', 'Й' => 'Y', 'К' => 'K', |
83
|
|
|
'Л' => 'L', 'М' => 'M', 'Н' => 'N', |
84
|
|
|
'О' => 'O', 'П' => 'P', 'Р' => 'R', |
85
|
|
|
'С' => 'S', 'Т' => 'T', 'У' => 'U', |
86
|
|
|
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', |
87
|
|
|
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', |
88
|
|
|
'Ь' => '', 'Ы' => 'Y', 'Ъ' => '', |
89
|
|
|
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', |
90
|
|
|
]; |
91
|
|
|
return strtr($string, $converter); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $str |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function str2url(string $str): string |
99
|
|
|
{ |
100
|
|
|
$str = $this->rus2translit($str); |
101
|
|
|
$str = \strtolower($str); |
102
|
|
|
$str = \preg_replace('~[^-a-z0-9]+~u', '-', $str); |
103
|
|
|
$str = \trim($str, '-'); |
104
|
|
|
return $str; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $html |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function html2text(string $html): string |
112
|
|
|
{ |
113
|
|
|
$html = \str_replace( |
114
|
|
|
["\n", '<br>', '<br/>', '<br />', '</div>', '</p>', '</h1>', '</h2>', '</h3>'], |
115
|
|
|
[' ', "\n", "\n", "\n", "</div>\n", "</p>\n", "</h1>\n", "</h2>\n", "</h3>\n"], |
116
|
|
|
$html |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return \htmlspecialchars(\trim(\strip_tags($html))); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|