Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stringhelper.php
1<?php
2
4
7
13{
14 // utf8 https://www.w3.org/International/questions/qa-forms-utf-8.en
15 public const UTF8_REGEXP = '/(?:
16 [\x09\x0A\x0D\x20-\x7E] # ASCII
17 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
18 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
19 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
20 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
21 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
22 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
23 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
24 )+/xs';
25
32 public static function getLength($str, $encoding = null)
33 {
34 if (Translate\Config::isUtfMode())
35 {
36 if (empty($encoding))
37 {
38 $encoding = Main\Localization\Translation::getCurrentEncoding();
39 }
40 return \mb_strlen($str, $encoding);
41 }
42
43 return \strlen($str);
44 }
45
54 public static function getSubstring($str, $start, $length, $encoding = null)
55 {
56 if (Translate\Config::isUtfMode())
57 {
58 if (empty($encoding))
59 {
60 $encoding = Main\Localization\Translation::getCurrentEncoding();
61 }
62 return \mb_substr($str, $start, $length, $encoding);
63 }
64
65 return \substr($str, $start, $length);
66 }
67
76 public static function getPosition($haystack, $needle, $offset = 0, $encoding = null)
77 {
78 if (\function_exists('mb_strpos'))
79 {
80 if (empty($encoding))
81 {
82 $encoding = Main\Localization\Translation::getCurrentEncoding();
83 }
84 return \mb_strpos($haystack, $needle, $offset, $encoding);
85 }
86
87 return \strpos($haystack, $needle, $offset);
88 }
89
96 public static function changeCaseToLower($str, $encoding = null)
97 {
98 if (\function_exists('mb_strtolower'))
99 {
100 if (empty($encoding))
101 {
102 $encoding = Main\Localization\Translation::getCurrentEncoding();
103 }
104 return \mb_strtolower($str, $encoding);
105 }
106
107 return \mb_strtolower($str);
108 }
109
116 public static function changeCaseToUpper($str, $encoding = null)
117 {
118 if (\function_exists('mb_strtoupper'))
119 {
120 if (empty($encoding))
121 {
122 $encoding = Main\Localization\Translation::getCurrentEncoding();
123 }
124 return \mb_strtoupper($str, $encoding);
125 }
126
127 return \mb_strtoupper($str);
128 }
129
137 public static function htmlSpecialChars($string, $flags = ENT_COMPAT, $encoding = null)
138 {
139 if (empty($encoding))
140 {
141 $encoding = Main\Localization\Translation::getCurrentEncoding();
142 }
143 return \htmlspecialchars($string, $flags, $encoding, true);
144 }
145
156 public static function validateUtf8OctetSequences($string)
157 {
158 return Main\Text\Encoding::detectUtf8($string, false);
159 }
160
170 public static function escapePhp($str, $enclosure = '"', $additional = ''): string
171 {
172 $w = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
173 //Lookaround negative lookbehind (?<!ASD)
174 if ($enclosure === "'")
175 {
176 $str = \preg_replace("/((?<![\\\\])['{$additional}]{1})/", "\\\\$1", $str);
177 // \${end of str} -> \\
178 $str = \preg_replace("/((?<![\\\\])\\\\‍)$/", "\\\\$1", $str);
179 }
180 elseif ($enclosure === '"')
181 {
182 // " -> \"
183 $str = \preg_replace("/((?<![\\\\])[\"{$additional}]{1})/", "\\\\$1", $str);
184 // $x -> \$x
185 $str = \preg_replace("/((?<![\\\\])[\$]{1}$w)/", "\\\\$1", $str);
186 // ${ -> \${
187 $str = \preg_replace("/((?<![\\\\])[\$]{1}\s*\{)/", "\\\\$1", $str);
188 // \${end of str} -> \\
189 $str = \preg_replace("/((?<![\\\\])\\\\‍)$/", "\\\\$1", $str);
190 }
191 elseif ($enclosure === '<<<')
192 {
193 // $x -> \$x
194 $str = \preg_replace("/((?<![\\\\])[\$]{1}$w)/", "\\\\$1", $str);
195 // ${ -> \${
196 $str = \preg_replace("/((?<![\\\\])[\$]{1}\s*\{)/", "\\\\$1", $str);
197 }
198
199 return $str;
200 }
201
210 public static function unescapePhp($str, $enclosure = '"'): string
211 {
212 //Lookaround positive lookbehind (?<=ASD)
213 // (?<=[\\]+)['\"\\\$]{1}
214
215 if ($enclosure == "'")
216 {
217 $from = ["\\'"];
218 $to = ["'"];
219 }
220 else
221 {
222 $from = ["\\\$", "\\\""];
223 $to = ["\$", "\""];
224 }
225
226 return \str_replace($from, $to, $str);
227 }
228
235 public static function hasPhpTokens($str, $enclosure = '"'): bool
236 {
237 $result = false;
238 if (!empty($str) && is_string($str))
239 {
240 if ($enclosure == '<<<')
241 {
242 $validTokens = [\T_CONSTANT_ENCAPSED_STRING, \T_START_HEREDOC, \T_ENCAPSED_AND_WHITESPACE, \T_END_HEREDOC];
243 $validChars = [];
244 $tokens = \token_get_all('<'. "?php \$MESS = <<<'HTML'\n". $str. "\nHTML;");
245 }
246 else
247 {
248 $validTokens = [\T_CONSTANT_ENCAPSED_STRING];
249 $validChars = [$enclosure];
250 $tokens = \token_get_all('<'. '?php $MESS = '. $enclosure. $str. $enclosure . ';');
251 }
252 $cnt = count($tokens);
253 if ($cnt <= 5 || $cnt > 10)
254 {
255 return true;
256 }
257
258 for ($inx = 5, $cnt--; $inx < $cnt ; $inx++)
259 {
260 $token = $tokens[$inx];
261 if (is_array($token))
262 {
263 $token[] = \token_name($token[0]);
264 if (!in_array($token[0], $validTokens))
265 {
266 $result = true;
267 break;
268 }
269 }
270 elseif (is_string($token))
271 {
272 if (!in_array($token, $validChars))
273 {
274 $result = true;
275 break;
276 }
277 }
278
279 }
280 }
281
282 return $result;
283 }
284}
static unescapePhp($str, $enclosure='"')
static changeCaseToLower($str, $encoding=null)
static getLength($str, $encoding=null)
static changeCaseToUpper($str, $encoding=null)
static getPosition($haystack, $needle, $offset=0, $encoding=null)
static getSubstring($str, $start, $length, $encoding=null)
static escapePhp($str, $enclosure='"', $additional = '')
static htmlSpecialChars($string, $flags=ENT_COMPAT, $encoding=null)
static hasPhpTokens($str, $enclosure='"')