up
This commit is contained in:
@@ -106,14 +106,25 @@ class BancolombiaParser
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elimina etiquetas HTML y decodifica entidades.
|
* Elimina completamente bloques <style>, <script>, <head> y luego
|
||||||
|
* convierte etiquetas estructurales en saltos de línea antes de strip_tags.
|
||||||
*/
|
*/
|
||||||
private static function stripHtml(string $text): string
|
private static function stripHtml(string $text): string
|
||||||
{
|
{
|
||||||
// Reemplazar <br>, <p>, <div>, <tr>, <li> con salto de línea para preservar estructura
|
// Eliminar bloques cuyo contenido no es texto visible
|
||||||
$text = preg_replace('/<(br\s*\/?|\/p|\/div|\/tr|\/li)>/i', "\n", $text);
|
$text = preg_replace('/<style[^>]*>.*?<\/style>/si', '', $text);
|
||||||
|
$text = preg_replace('/<script[^>]*>.*?<\/script>/si', '', $text);
|
||||||
|
$text = preg_replace('/<head[^>]*>.*?<\/head>/si', '', $text);
|
||||||
|
|
||||||
|
// Convertir etiquetas estructurales a saltos de línea
|
||||||
|
$text = preg_replace('/<(br\s*\/?\s*|\/p|\/div|\/tr|\/li|\/td)>/i', "\n", $text);
|
||||||
|
|
||||||
|
// Quitar todas las etiquetas restantes
|
||||||
$text = strip_tags($text);
|
$text = strip_tags($text);
|
||||||
|
|
||||||
|
// Decodificar entidades HTML
|
||||||
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ class CorreoImapService
|
|||||||
|
|
||||||
private function cleanBody(string $body): string
|
private function cleanBody(string $body): string
|
||||||
{
|
{
|
||||||
// Quitar quoted-printable si aplica
|
// Quitar quoted-printable si aplica (antes de tocar HTML)
|
||||||
if (str_contains($body, '=\r\n') || preg_match('/=[0-9A-F]{2}/i', $body)) {
|
if (str_contains($body, '=\r\n') || preg_match('/=[0-9A-F]{2}/i', $body)) {
|
||||||
$body = quoted_printable_decode($body);
|
$body = quoted_printable_decode($body);
|
||||||
}
|
}
|
||||||
@@ -302,12 +302,9 @@ class CorreoImapService
|
|||||||
// Eliminar líneas de cabecera de parte MIME (Content-Type, etc.)
|
// Eliminar líneas de cabecera de parte MIME (Content-Type, etc.)
|
||||||
$body = preg_replace('/^(Content-[^\r\n]+\r?\n)+/mi', '', $body);
|
$body = preg_replace('/^(Content-[^\r\n]+\r?\n)+/mi', '', $body);
|
||||||
|
|
||||||
// Si el cuerpo contiene HTML, convertir bloques estructurales a saltos
|
// Si el cuerpo contiene HTML, procesarlo adecuadamente
|
||||||
// de línea y luego quitar todas las etiquetas para obtener texto plano
|
|
||||||
if (preg_match('/<[a-z][\s>]/i', $body)) {
|
if (preg_match('/<[a-z][\s>]/i', $body)) {
|
||||||
$body = preg_replace('/<(br\s*\/?|\/p|\/div|\/tr|\/li)>/i', "\n", $body);
|
$body = self::stripHtmlFull($body);
|
||||||
$body = strip_tags($body);
|
|
||||||
$body = html_entity_decode($body, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalizar saltos de línea
|
// Normalizar saltos de línea
|
||||||
@@ -318,4 +315,30 @@ class CorreoImapService
|
|||||||
|
|
||||||
return trim($body);
|
return trim($body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Elimina completamente bloques <style>, <script>, <head> y luego
|
||||||
|
* convierte etiquetas estructurales en saltos de línea antes de strip_tags.
|
||||||
|
*/
|
||||||
|
private static function stripHtmlFull(string $html): string
|
||||||
|
{
|
||||||
|
// 1. Eliminar bloques cuyo contenido no es texto visible
|
||||||
|
$html = preg_replace('/<style[^>]*>.*?<\/style>/si', '', $html);
|
||||||
|
$html = preg_replace('/<script[^>]*>.*?<\/script>/si', '', $html);
|
||||||
|
$html = preg_replace('/<head[^>]*>.*?<\/head>/si', '', $html);
|
||||||
|
|
||||||
|
// 2. Convertir saltos estructurales a \n
|
||||||
|
$html = preg_replace('/<(br\s*\/?\s*|\/p|\/div|\/tr|\/li|\/td)>/i', "\n", $html);
|
||||||
|
|
||||||
|
// 3. Quitar todas las etiquetas restantes
|
||||||
|
$html = strip_tags($html);
|
||||||
|
|
||||||
|
// 4. Decodificar entidades HTML (& etc.)
|
||||||
|
$html = html_entity_decode($html, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||||
|
|
||||||
|
// 5. Limpiar espacios por línea y líneas en blanco excesivas
|
||||||
|
$lines = array_map('trim', explode("\n", $html));
|
||||||
|
$lines = array_filter($lines, fn($l) => $l !== '');
|
||||||
|
return implode("\n", $lines);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user