60 ? substr($url, 0, 57) . '...' : $url;
return '' . $displayUrl . '';
}, $text);
// 3. Converte quebras de linha em
$text = nl2br($text);
return $text;
}
/**
* Verifica se o conteúdo parece ser HTML (do Quill editor)
*
* @param string $text Texto a verificar
* @return bool True se parece ser HTML
*/
private static function isHtmlContent(string $text): bool
{
// Verifica se contém tags HTML comuns do Quill
return preg_match('/<(p|div|br|strong|em|u|a|ul|ol|li|h[1-6]|span|blockquote)[^>]*>/i', $text) === 1;
}
/**
* Purifica HTML mantendo apenas tags seguras
*
* @param string $html HTML a ser purificado
* @return string HTML purificado
*/
private static function purifyHtml(string $html): string
{
// Tags permitidas do Quill editor
$allowedTags = '
';
// Remove tags não permitidas
$html = strip_tags($html, $allowedTags);
// Adiciona target="_blank" e rel="noopener" em links para segurança
$html = preg_replace_callback(
'/]*href=["\'][^"\']+["\'][^>]*)>/i',
function ($matches) {
$attrs = $matches[1];
// Remove atributos target e rel existentes
$attrs = preg_replace('/\s*(target|rel)=["\'][^"\']*["\']/i', '', $attrs);
return '';
},
$html
);
return $html;
}
/**
* Limpa e normaliza quebras de linha no texto.
* Útil para normalizar dados antes de salvar no banco.
*
* @param string|null $text Texto a ser normalizado
* @return string Texto com quebras de linha normalizadas
*/
public static function normalizeLineBreaks(?string $text): string
{
if (empty($text)) {
return '';
}
// Normaliza todas as variações de quebra de linha para \n
$text = str_replace(["\r\n", "\r"], "\n", $text);
return $text;
}
}