1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class joosText
17: {
18: 19: 20: 21: 22:
23: public static $abc_ru = array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Э', 'Ю', 'Я');
24:
25: 26: 27: 28: 29:
30: public static $abc_en = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
31:
32: 33: 34: 35: 36: 37: 38:
39: public static function declension($int, $expressions)
40: {
41: if (count($expressions) < 3) {
42: $expressions[2] = $expressions[1];
43: }
44:
45: settype($int, 'integer');
46: $count = $int % 100;
47: if ($count >= 5 && $count <= 20) {
48: $result = $expressions['2'];
49: } else {
50: $count = $count % 10;
51: if ($count == 1) {
52: $result = $expressions['0'];
53: } elseif ($count >= 2 && $count <= 4) {
54: $result = $expressions['1'];
55: } else {
56: $result = $expressions['2'];
57: }
58: }
59:
60: return $result;
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public static function word_limiter($str, $limit = 100, $end_char = '…')
73: {
74: if (joosString::trim($str) == '') {
75: return $str;
76: }
77:
78: preg_match('/^\s*+(?:\S++\s*+){1,' . (int) $limit . '}/u', $str, $matches);
79:
80: $end_char = (joosString::strlen($str) == joosString::strlen($matches[0])) ? '' : $end_char;
81:
82: return joosString::rtrim($matches[0]) . $end_char;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92: 93: 94:
95: public static function character_limiter($str, $limit = 500, $end_char = '…', $max_word_lench = 500)
96: {
97: if (joosString::strlen($str) < $limit) {
98: return $str;
99: }
100:
101: $str = preg_replace("/\s+/u", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
102:
103: if (joosString::strlen($str) <= $limit) {
104: return $str;
105: }
106:
107: $out = "";
108: foreach (explode(' ', joosString::trim($str)) as $val) {
109: if (joosString::strlen($val) > $max_word_lench) {
110: $val = joosString::substr($val, 0, $max_word_lench) . $end_char;
111: }
112: $out .= $val . ' ';
113:
114: if (joosString::strlen($out) >= $limit) {
115: $out = joosString::trim($out);
116:
117: return (joosString::strlen($out) == joosString::strlen($str)) ? $out : $out . $end_char;
118: }
119: }
120:
121: return joosString::substr($str, 0, $limit) . $end_char;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public static function text_censor($str, array $censored, $replacement = '')
134: {
135: $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
136:
137: foreach ($censored as $badword) {
138: if ($replacement != '') {
139: $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/iu", "\\1{$replacement}\\3", $str);
140: } else {
141: $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ieu", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str);
142: }
143: }
144:
145: return joosString::trim($str);
146: }
147:
148: 149: 150: 151: 152: 153: 154:
155: public static function text_msword_clean($text)
156: {
157: $text = str_replace(" ", "", $text);
158: $text = str_replace("</html>", "", $text);
159: $text = preg_replace("/FONT-SIZE: [0-9]+pt;/miu", "", $text);
160:
161: return preg_replace("/([ \f\r\t\n\'\"])on[a-z]+=[^>]+/iu", "\\1", $text);
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public static function semantic_replacer($text)
172: {
173: $text = preg_replace("!<b>(.*?)</b>!si", "<strong>\\1</strong>", $text);
174: $text = preg_replace("!<i>(.*?)</i>!si", "<em>\\1</em>", $text);
175: $text = preg_replace("!<u>(.*?)</u>!si", "<strike>\\1</strike>", $text);
176:
177: return str_replace("<br>", "<br />", $text);
178: }
179:
180: 181: 182: 183: 184: 185: 186:
187: public static function simple_clean($text)
188: {
189: $text = html_entity_decode($text, ENT_QUOTES, 'utf-8');
190:
191: return self::text_clean($text);
192: }
193:
194: 195: 196: 197: 198: 199: 200:
201: public static function text_clean($text)
202: {
203: $text = preg_replace("'<script[^>]*>.*?</script>'si", '', $text);
204: $text = preg_replace('/<!--.+?-->/', '', $text);
205: $text = preg_replace('/{.+?}/', '', $text);
206: $text = preg_replace('/ /', ' ', $text);
207: $text = preg_replace('/&/', ' ', $text);
208: $text = preg_replace('/"/', ' ', $text);
209: $text = strip_tags($text);
210:
211: return htmlspecialchars($text, null, 'UTF-8');
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225:
226: public static function outlink_parse($text)
227: {
228: $host = str_replace(array('http://', 'www.'), '', JPATH_SITE);
229: $host = str_replace('.', '\.', $host);
230:
231:
232: return preg_replace('/href="?(http:\/\/(?!(www\.|)' . $host . ')([^">\s]*))/ie', "'href=\"" . JPATH_SITE . "/out.php?url=' . urlencode('\$1') . '\"'", $text);
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242:
243: public static function russian_transliterate($string)
244: {
245: $converter = array('а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', 'ь' => '\'', 'ы' => 'y', 'ъ' => '\'', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', 'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'', 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya');
246:
247: return strtr($string, $converter);
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259:
260: public static function text_to_url($str)
261: {
262:
263: $str = str_ireplace(array('ь', 'ъ'), '', $str);
264:
265: $str = self::russian_transliterate($str);
266:
267: $str = strtolower($str);
268:
269: $str = str_replace(array("'", '-', '"', '`'), ' ', $str);
270: $str = preg_replace('/[^a-z0-9\-]+/', '-', $str);
271:
272: return trim($str, '-');
273: }
274:
275: 276: 277: 278: 279: 280: 281: 282:
283: public static function text_wrap($text, $max_length = 30)
284: {
285: $counter = 0;
286: $newText = array();
287: $array = array();
288:
289: $textLength = joosString::strlen($text);
290:
291: for ($i = 0; $i <= $textLength; $i++) {
292: $array[] = joosString::substr($text, $i, 1);
293: }
294:
295: $textLength = count($array);
296:
297: for ($x = 0; $x < $textLength; $x++) {
298: if (preg_match("/[[:space:]]/u", $array[$x])) {
299: $counter = 0;
300: } else {
301: $counter++;
302: }
303:
304: $newText[] = $array[$x];
305:
306: if ($counter >= $max_length) {
307: $newText[] = '<wbr style="display: inline-block"/>­';
308: $counter = 0;
309: }
310: }
311:
312: return implode('', $newText);
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: public static function to_canonical($text)
323: {
324:
325: $text = joosString:: strtolower($text);
326:
327:
328: $to_del = array('~', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '|', '?', ',', '.', '/', ';', ':', '"', "'", '№', ' ', ' ');
329: $text = str_replace($to_del, '', $text);
330:
331:
332: $a = array('о', 'o', 'l', 'L', '|', '!', 'i', 'х', 's', 'а', 'р', 'с', 'в', 'к', 'е', 'й', 'ё', 'ш', 'з', 'у', 'т', 'д', 'd', 'ф', 'в', 'м', 'н', 'и', 'э', 'ь', 'ъ', 'ю');
333: $b = array('0', '0', '1', '1', '1', '1', '1', 'x', '$', '0', 'p', '$', 'b', 'k', 'e', 'и', 'е', 'щ', '$', 'y', 't', 't', 't', 'b', 'b', 'm', 'h', 'e', 'e', '', '', 'u');
334: $text = str_replace($a, $b, $text);
335:
336:
337: $return = $o = '';
338: $_l = joosString::strlen($text);
339: for ($i = 0; $i < $_l; $i++) {
340: $c = joosString::substr($text, $i, 1);
341: if ($c != $o) {
342: $return .= $c;
343: $o = $c;
344: }
345: }
346:
347: return $return;
348: }
349:
350: 351: 352: 353: 354: 355: 356: 357:
358: public function id_decode($string)
359: {
360: $chars = '23456789abcdeghkmnpqsuvxyzABCDEGHKLMNPQSUVXYZ';
361: $length = 45;
362: $size = strlen($string) - 1;
363: $array = str_split($string);
364: $id = strpos($chars, array_pop($array));
365: foreach ($array as $i => $char) {
366: $id += strpos($chars, $char) * pow($length, $size - $i);
367: }
368:
369: return $id;
370: }
371:
372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382:
383: public static function sexerate($sex, array $texts)
384: {
385: $sex = joosString::strtolower($sex);
386: $sex = strtr($sex, array('м' => 0, 'ж' => 1, 'm' => 0, 'f' => 1, 'муж' => 0, 'жен' => 1, 'male' => 0, 'female' => 1, 'мужчина' => 0, 'женщина' => 1,));
387:
388: return isset($texts[$sex]) ? $texts[$sex] : $texts[2];
389: }
390:
391: 392: 393: 394: 395: 396: 397: 398:
399: public static function json_encode($value)
400: {
401: $arr_replace_utf = array('\u0410', '\u0430', '\u0411', '\u0431', '\u0412', '\u0432', '\u0413', '\u0433', '\u0414', '\u0434', '\u0415', '\u0435', '\u0401', '\u0451', '\u0416', '\u0436', '\u0417', '\u0437', '\u0418', '\u0438', '\u0419', '\u0439', '\u041a', '\u043a', '\u041b', '\u043b', '\u041c', '\u043c', '\u041d', '\u043d', '\u041e', '\u043e', '\u041f', '\u043f', '\u0420', '\u0440', '\u0421', '\u0441', '\u0422', '\u0442', '\u0423', '\u0443', '\u0424', '\u0444', '\u0425', '\u0445', '\u0426', '\u0446', '\u0427', '\u0447', '\u0428', '\u0448', '\u0429', '\u0449', '\u042a', '\u044a', '\u042b', '\u044b', '\u042c', '\u044c', '\u042d', '\u044d', '\u042e', '\u044e', '\u042f', '\u044f');
402: $arr_replace_cyr = array('А', 'а', 'Б', 'б', 'В', 'в', 'Г', 'г', 'Д', 'д', 'Е', 'е', 'Ё', 'ё', 'Ж', 'ж', 'З', 'з', 'И', 'и', 'Й', 'й', 'К', 'к', 'Л', 'л', 'М', 'м', 'Н', 'н', 'О', 'о', 'П', 'п', 'Р', 'р', 'С', 'с', 'Т', 'т', 'У', 'у', 'Ф', 'ф', 'Х', 'х', 'Ц', 'ц', 'Ч', 'ч', 'Ш', 'ш', 'Щ', 'щ', 'Ъ', 'ъ', 'Ы', 'ы', 'Ь', 'ь', 'Э', 'э', 'Ю', 'ю', 'Я', 'я');
403: $str1 = json_encode($value);
404: $str2 = str_replace($arr_replace_utf, $arr_replace_cyr, $str1);
405:
406: return $str2;
407: }
408:
409: }
410: