1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
stringhelper.php
См. документацию.
1<?php
2
4
7
11
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 {
35 {
36 if (empty($encoding))
37 {
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 {
57 {
58 if (empty($encoding))
59 {
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 {
83 }
84 return \mb_strpos($haystack, $needle, $offset, $encoding);
85 }
86
87 return \strpos($haystack, $needle, $offset);
88 }
89
95 public static function changeCaseToLower($str)
96 {
97 return \mb_strtolower($str);
98 }
99
105 public static function changeCaseToUpper($str)
106 {
107 return \mb_strtoupper($str);
108 }
109
117 public static function htmlSpecialChars($string, $flags = ENT_COMPAT, $encoding = null)
118 {
119 if (empty($encoding))
120 {
122 }
123 return \htmlspecialchars($string, $flags, $encoding, true);
124 }
125
136 public static function validateUtf8OctetSequences($string)
137 {
138 return Main\Text\Encoding::detectUtf8($string, false);
139 }
140
150 public static function escapePhp($str, $enclosure = '"', $additional = ''): string
151 {
152 $w = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
153 //Lookaround negative lookbehind (?<!ASD)
154 if ($enclosure === "'")
155 {
156 $str = \preg_replace("/((?<![\\\\])['{$additional}]{1})/", "\\\\$1", $str);
157 // \${end of str} -> \\
158 $str = \preg_replace("/((?<![\\\\])\\\\‍)$/", "\\\\$1", $str);
159 }
160 elseif ($enclosure === '"')
161 {
162 // " -> \"
163 $str = \preg_replace("/((?<![\\\\])[\"{$additional}]{1})/", "\\\\$1", $str);
164 // $x -> \$x
165 $str = \preg_replace("/((?<![\\\\])[\$]{1}$w)/", "\\\\$1", $str);
166 // ${ -> \${
167 $str = \preg_replace("/((?<![\\\\])[\$]{1}\s*\{)/", "\\\\$1", $str);
168 // \${end of str} -> \\
169 $str = \preg_replace("/((?<![\\\\])\\\\‍)$/", "\\\\$1", $str);
170 }
171 elseif ($enclosure === '<<<')
172 {
173 // $x -> \$x
174 $str = \preg_replace("/((?<![\\\\])[\$]{1}$w)/", "\\\\$1", $str);
175 // ${ -> \${
176 $str = \preg_replace("/((?<![\\\\])[\$]{1}\s*\{)/", "\\\\$1", $str);
177 }
178
179 return $str;
180 }
181
190 public static function unescapePhp($str, $enclosure = '"'): string
191 {
192 //Lookaround positive lookbehind (?<=ASD)
193 // (?<=[\\]+)['\"\\\$]{1}
194
195 if ($enclosure == "'")
196 {
197 $from = ["\\'"];
198 $to = ["'"];
199 }
200 else
201 {
202 $from = ["\\\$", "\\\""];
203 $to = ["\$", "\""];
204 }
205
206 return \str_replace($from, $to, $str);
207 }
208
215 public static function hasPhpTokens($str, $enclosure = '"'): bool
216 {
217 $result = false;
218 if (!empty($str) && is_string($str))
219 {
220 if ($enclosure == '<<<')
221 {
222 $validTokens = [\T_CONSTANT_ENCAPSED_STRING, \T_START_HEREDOC, \T_ENCAPSED_AND_WHITESPACE, \T_END_HEREDOC];
223 $validChars = [];
224 $tokens = \token_get_all('<'. "?php \$MESS = <<<'HTML'\n". $str. "\nHTML;");
225 }
226 else
227 {
228 $validTokens = [\T_CONSTANT_ENCAPSED_STRING];
229 $validChars = [$enclosure];
230 $tokens = \token_get_all('<'. '?php $MESS = '. $enclosure. $str. $enclosure . ';');
231 }
232 $cnt = count($tokens);
233 if ($cnt <= 5 || $cnt > 10)
234 {
235 return true;
236 }
237
238 for ($inx = 5, $cnt--; $inx < $cnt ; $inx++)
239 {
240 $token = $tokens[$inx];
241 if (is_array($token))
242 {
243 $token[] = \token_name($token[0]);
244 if (!in_array($token[0], $validTokens))
245 {
246 $result = true;
247 break;
248 }
249 }
250 elseif (is_string($token))
251 {
252 if (!in_array($token, $validChars))
253 {
254 $result = true;
255 break;
256 }
257 }
258
259 }
260 }
261
262 return $result;
263 }
264}
static isUtfMode()
Определения application.php:726
static getCurrentEncoding()
Определения translation.php:104
static detectUtf8($string, $replaceHex=true)
Определения encoding.php:154
static changeCaseToLower($str)
Определения stringhelper.php:95
static unescapePhp($str, $enclosure='"')
Определения stringhelper.php:190
static getLength($str, $encoding=null)
Определения stringhelper.php:32
static getPosition($haystack, $needle, $offset=0, $encoding=null)
Определения stringhelper.php:76
static validateUtf8OctetSequences($string)
Определения stringhelper.php:136
static getSubstring($str, $start, $length, $encoding=null)
Определения stringhelper.php:54
static escapePhp($str, $enclosure='"', $additional = '')
Определения stringhelper.php:150
static changeCaseToUpper($str)
Определения stringhelper.php:105
static htmlSpecialChars($string, $flags=ENT_COMPAT, $encoding=null)
Определения stringhelper.php:117
static hasPhpTokens($str, $enclosure='"')
Определения stringhelper.php:215
$str
Определения commerceml2.php:63
$result
Определения get_property_values.php:14
$start
Определения get_search.php:9
Определения autoload.php:3
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936