/', function($matches) { return str_repeat(' ', 30); }, $text); // Проверка длины текста if (mb_strlen($text) <= $limit) { return $text; } // Разделение текста на видимую и скрытую части $visibleText = mb_substr($text, 0, $limit); $hiddenText = mb_substr($text, $limit); // Убедиться, что видимый текст не обрывается на полуслове $lastSpace = mb_strrpos($visibleText, ' '); if ($lastSpace !== false) { $visibleText = mb_substr($visibleText, 0, $lastSpace); $hiddenText = mb_substr($text, $lastSpace); } // Генерация HTML return self::generateHtml($visibleText, $hiddenText); } private static function generateHtml($visibleText, $hiddenText) { return << Показать ещё HTML; } public static function i($var) { $search = array( '/\[REPLY\=(.*?)\](.*?)\[\/REPLY\]/is', '/\*\*([^\*]+)\*\*/', '/\_\_([^\*]+)\_\_/', '/\[u\](.*?)\[\/u\]/is', '/\+\+([^\*]+)\+\+/', '/\~\~([^\*]+)\~\~/', '/\[link\=(.*?)\](.*?)\[\/link\]/is', '/\[br\]/is', '/\[image\](.*?)\[\/image\]/is', '/\[video\](.*?)\[\/video\]/is', '/\[music\](.*?)\[\/music\]/is', ); $replace = array( '$2', '$1', '$1', '$1', '$1', '$1', '$2', '
', '$2', "", "", ); $var = preg_replace($search, $replace, $var); return $var; } public static function formatText($text) { $text = self::formatLinks($text); $text = self::formatEmojis($text); return $text; } public static function formatEmojis(string $text): string { $emojis = \Emoji\detect_emoji($text); $replaced = []; foreach ($emojis as $emoji) { $point = explode("-", strtolower($emoji["hex_str"]))[0]; if (in_array($point, $replaced)) { continue; } else { $replaced[] = $point; } $image = "https://cdnjs.cloudflare.com/ajax/libs/emoji-datasource-apple/15.1.0/img/apple/64/$point.png"; $image = "$emoji[emoji] '%' . substr($word, 1) . '%'))[0]['username'], substr($word, 1)) === 0) { $newstring .= "" . $word . " "; } else if (strcasecmp(DB::query('SELECT linkgroup FROM `groups` WHERE (LOWER(linkgroup) LIKE :username)', array(':username' => '%' . substr($word, 1) . '%'))[0]['linkgroup'], substr($word, 1)) === 0) { $newstring .= "" . $word . " "; } else { $newstring .= $word . " "; } } else if (substr($word, 0, 1) == "#") { $newstring .= "" . htmlspecialchars($word) . " "; } else { $newstring .= $word . " "; } } return $newstring; } public static function formatLinks($text) { $currentDomain = $_SERVER['HTTP_HOST']; $pattern = '/(https?:\/\/[^\s<]+)/i'; $imgPattern = '/]*src="https?:\/\/[^\s<]*"[^>]*>/i'; preg_match_all($imgPattern, $text, $imgMatches); $imgTags = $imgMatches[0]; $messageWithoutImgTags = preg_replace($imgPattern, '###IMG###', $text); $messageWithClickableLinks = preg_replace_callback($pattern, function ($matches) use ($currentDomain) { $url = $matches[0]; $urlComponents = parse_url($url); if ($urlComponents['host'] === $currentDomain && preg_match('/^\/addemoji\/([\w-]+)/', $urlComponents['path'], $idMatches)) { $id = $idMatches[1]; return '' . $url . ''; } return '' . $url . ''; }, $messageWithoutImgTags); foreach ($imgTags as $imgTag) { $messageWithClickableLinks = preg_replace('/###IMG###/', $imgTag, $messageWithClickableLinks, 1); } return $messageWithClickableLinks; } }