Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
utfsafestring.php
1<?php
8namespace Bitrix\Main\Text;
9
11
13{
14 public static function getLastPosition($haystack, $needle)
15 {
17 {
18 //mb_strrpos does not work on invalid UTF-8 strings
19 $ln = mb_strlen($needle);
20 for ($i = mb_strlen($haystack) - $ln; $i >= 0; $i--)
21 {
22 if (mb_substr($haystack, $i, $ln) == $needle)
23 {
24 return $i;
25 }
26 }
27 return false;
28 }
29
30 return mb_strrpos($haystack, $needle);
31 }
32
33 public static function rtrimInvalidUtf($string)
34 {
35 //valid UTF-8 octet sequences
36 //0xxxxxxx
37 //110xxxxx 10xxxxxx
38 //1110xxxx 10xxxxxx 10xxxxxx
39 //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
40
41 $last4bytes = substr($string, -3);
42 $reversed = array_reverse(unpack("C*", $last4bytes));
43 if (($reversed[0] & 0x80) === 0x00) //ASCII
44 return $string;
45 elseif (($reversed[0] & 0xC0) === 0xC0) //Start of utf seq (cut it!)
46 return substr($string, 0, -1);
47 elseif (($reversed[1] & 0xE0) === 0xE0) //Start of utf seq (longer than 2 bytes)
48 return substr($string, 0, -2);
49 elseif (($reversed[2] & 0xE0) === 0xF0) //Start of utf seq (longer than 3 bytes)
50 return substr($string, 0, -3);
51 return $string;
52 }
53
60 public static function escapeInvalidUtf($string)
61 {
62 $escape = function($matches)
63 {
64 return (isset($matches[2])? '?' : $matches[1]);
65 };
66
67 return preg_replace_callback('/([\x00-\x7F]+
68 |[\xC2-\xDF][\x80-\xBF]
69 |\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF])
70 |([\x80-\xFF])/x', $escape, $string
71 );
72 }
73
74
85 public static function pad($string, $padLen, $padStr = ' ', $padType = STR_PAD_RIGHT)
86 {
87 $strLength = mb_strlen($string);
88 $padStrLength = mb_strlen($padStr);
89 if (!$strLength && ($padType == STR_PAD_RIGHT || $padType == STR_PAD_LEFT))
90 {
91 $strLength = 1; // @debug
92 }
93 if (!$padLen || !$padStrLength || $padLen <= $strLength)
94 {
95 return $string;
96 }
97
98 $result = null;
99 $repeat = ceil(($padLen - $strLength) / $padStrLength);
100 if ($padType == STR_PAD_RIGHT)
101 {
102 $result = $string . str_repeat($padStr, $repeat);
103 $result = mb_substr($result, 0, $padLen);
104 }
105 else if ($padType == STR_PAD_LEFT)
106 {
107 $result = str_repeat($padStr, $repeat) . $string;
108 $result = mb_substr($result, -$padLen);
109 }
110 else if ($padType == STR_PAD_BOTH)
111 {
112 $length = ($padLen - $strLength) / 2;
113 $repeat = ceil($length / $padStrLength);
114 $result = mb_substr(str_repeat($padStr, $repeat), 0, floor($length))
115 .$string
116 .mb_substr(str_repeat($padStr, $repeat), 0, ceil($length));
117 }
118
119 return $result;
120 }
121
130 public static function checkEncoding($data)
131 {
133 {
134 return true;
135 }
136
137 if (!is_string($data) && !is_array($data))
138 {
139 return true;
140 }
141
142 if (is_string($data))
143 {
144 return mb_check_encoding($data);
145 }
146
147 foreach ($data as $value)
148 {
149 if (!static::checkEncoding($value))
150 {
151 return false;
152 }
153 }
154
155 return true;
156 }
157}
static getLastPosition($haystack, $needle)
static pad($string, $padLen, $padStr=' ', $padType=STR_PAD_RIGHT)