1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
utfsafestring.php
См. документацию.
1<?php
8namespace Bitrix\Main\Text;
9
11{
12 public static function getLastPosition($haystack, $needle)
13 {
14 if (mb_check_encoding((string)$haystack))
15 {
16 return mb_strrpos((string)$haystack, $needle);
17 }
18 return false;
19 }
20
21 public static function rtrimInvalidUtf($string)
22 {
23 //valid UTF-8 octet sequences
24 //0xxxxxxx
25 //110xxxxx 10xxxxxx
26 //1110xxxx 10xxxxxx 10xxxxxx
27 //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
28
29 $last4bytes = substr($string, -3);
30 $reversed = array_reverse(unpack("C*", $last4bytes));
31 if (($reversed[0] & 0x80) === 0x00) //ASCII
32 return $string;
33 elseif (($reversed[0] & 0xC0) === 0xC0) //Start of utf seq (cut it!)
34 return substr($string, 0, -1);
35 elseif (($reversed[1] & 0xE0) === 0xE0) //Start of utf seq (longer than 2 bytes)
36 return substr($string, 0, -2);
37 elseif (($reversed[2] & 0xE0) === 0xF0) //Start of utf seq (longer than 3 bytes)
38 return substr($string, 0, -3);
39 return $string;
40 }
41
48 public static function escapeInvalidUtf($string)
49 {
50 $escape = function($matches)
51 {
52 return (isset($matches[2])? '?' : $matches[1]);
53 };
54
55 return preg_replace_callback('/([\x00-\x7F]+
56 |[\xC2-\xDF][\x80-\xBF]
57 |\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF])
58 |([\x80-\xFF])/x', $escape, $string
59 );
60 }
61
62
73 public static function pad($string, $padLen, $padStr = ' ', $padType = STR_PAD_RIGHT)
74 {
75 $strLength = mb_strlen($string);
76 $padStrLength = mb_strlen($padStr);
77 if (!$strLength && ($padType == STR_PAD_RIGHT || $padType == STR_PAD_LEFT))
78 {
79 $strLength = 1; // @debug
80 }
81 if (!$padLen || !$padStrLength || $padLen <= $strLength)
82 {
83 return $string;
84 }
85
86 $result = null;
87 $repeat = ceil(($padLen - $strLength) / $padStrLength);
88 if ($padType == STR_PAD_RIGHT)
89 {
90 $result = $string . str_repeat($padStr, $repeat);
91 $result = mb_substr($result, 0, $padLen);
92 }
93 else if ($padType == STR_PAD_LEFT)
94 {
95 $result = str_repeat($padStr, $repeat) . $string;
96 $result = mb_substr($result, -$padLen);
97 }
98 else if ($padType == STR_PAD_BOTH)
99 {
100 $length = ($padLen - $strLength) / 2;
101 $repeat = ceil($length / $padStrLength);
102 $result = mb_substr(str_repeat($padStr, $repeat), 0, floor($length))
103 .$string
104 .mb_substr(str_repeat($padStr, $repeat), 0, ceil($length));
105 }
106
107 return $result;
108 }
109
118 public static function checkEncoding($data)
119 {
120 if (!is_string($data) && !is_array($data))
121 {
122 return true;
123 }
124
125 return mb_check_encoding($data);
126 }
127}
static escapeInvalidUtf($string)
Определения utfsafestring.php:48
static checkEncoding($data)
Определения utfsafestring.php:118
static rtrimInvalidUtf($string)
Определения utfsafestring.php:21
static getLastPosition($haystack, $needle)
Определения utfsafestring.php:12
static pad($string, $padLen, $padStr=' ', $padType=STR_PAD_RIGHT)
Определения utfsafestring.php:73
$data['IS_AVAILABLE']
Определения .description.php:13
$result
Определения get_property_values.php:14
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$matches
Определения index.php:22