$title, 'excerpt' => self::plainLine($excerpt, self::EXCERPT_MAX), 'body' => self::toHtml($body)]; } /** Plain single line: tags removed, whitespace collapsed, bounded. */ public static function plainLine(string $text, int $max): string { $text = (string) preg_replace('/<[^>]*>?/u', '', $text); $text = trim((string) preg_replace('/\s+/u', ' ', $text)); return mb_substr($text, 0, $max); } public static function toHtml(string $text): string { $text = mb_substr(trim($text), 0, self::BODY_MAX); if ($text === '') { return ''; } $html = ''; $para = []; $list = []; $flush = static function () use (&$html, &$para, &$list): void { if ($para !== []) { $html .= '
' . implode(' ', $para) . "
\n"; $para = []; } if ($list !== []) { $html .= "