sanitize($html)); } /** * Übernimmt Klartext aus dem Altbestand: Absätze an Leerzeilen, Zeichen maskiert. */ public static function fromPlainText(?string $text): string { $text = trim((string) $text); if ($text === '') { return ''; } $paragraphs = preg_split("/\n\s*\n/", $text) ?: []; return implode('', array_map( fn (string $paragraph): string => '
'.e(trim($paragraph)).'
', $paragraphs, )); } public static function isBlank(?string $html): bool { return trim(html_entity_decode(strip_tags((string) $html), ENT_QUOTES | ENT_HTML5, 'UTF-8')) === ''; } public static function excerpt(?string $html, int $length = 80): string { $text = trim(preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags(str_replace('<', ' <', (string) $html)), ENT_QUOTES | ENT_HTML5, 'UTF-8')) ?? ''); return mb_strlen($text) > $length ? rtrim(mb_substr($text, 0, $length - 1)).'…' : $text; } private static function sanitizer(): HtmlSanitizer { return self::$sanitizer ??= new HtmlSanitizer( (new HtmlSanitizerConfig) ->allowElement('p') ->allowElement('br') ->allowElement('strong') ->allowElement('b') ->allowElement('em') ->allowElement('i') ->allowElement('ul') ->allowElement('ol') ->allowElement('li') ->allowElement('a', ['href']) ->allowLinkSchemes(['http', 'https', 'mailto']) ->allowRelativeLinks() ->forceAttribute('a', 'rel', 'noopener') ->withMaxInputLength(40000), ); } }