set_charset("utf8mb4");
echo "========================================\n";
echo "ANÁLISIS DE CORRECCIÓN PROPUESTA\n";
echo "========================================\n\n";
// Patrón que encuentra:
TEXTO
// Este patrón captura:
// - $1: inicial (con espacios)
// - $2: espacios entre
y
// - $3: contenido del (ej: Texto)
// - $4: espacios entre y (\s*)(.*?)(\s*)#is';
$replacement = '$1- $3$4
';
echo "PATRÓN A BUSCAR:\n";
echo "
\\s*
\\s*CONTENIDO\\s*\n\n";
echo "REEMPLAZO:\n";
echo " - CONTENIDO
\n\n";
// Obtener HTML del post ID 3
$result = $conn->query("SELECT id, page, html FROM datos_seo_pagina WHERE id = 3");
$row = $result->fetch_assoc();
$html = $row["html"];
$page = $row["page"];
echo "PROBANDO CON POST ID 3:\n";
echo "URL: $page\n";
echo "────────────────────────────\n\n";
// Encontrar todas las ocurrencias
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
echo "Ocurrencias encontradas: " . count($matches) . "\n\n";
// Mostrar cada ocurrencia y su corrección propuesta
foreach (array_slice($matches, 0, 3) as $idx => $match) {
$full_match = $match[0][0];
$position = $match[0][1];
echo "[$idx] Posición: $position\n";
echo "ANTES:\n";
echo htmlspecialchars($full_match) . "\n\n";
$fixed = preg_replace($pattern, $replacement, $full_match);
echo "DESPUÉS:\n";
echo htmlspecialchars($fixed) . "\n";
echo "────────────────────────────\n\n";
}
// Aplicar corrección en memoria y contar diferencia
$html_fixed = preg_replace($pattern, $replacement, $html);
$before = preg_match_all($pattern, $html);
$after = preg_match_all($pattern, $html_fixed);
echo "========================================\n";
echo "RESUMEN DE CORRECCIÓN (sin aplicar):\n";
echo "========================================\n";
echo "Ocurrencias ANTES: $before\n";
echo "Ocurrencias DESPUÉS: $after\n";
echo "Reducción: " . ($before - $after) . "\n\n";
// Verificar que la estructura es válida después de la corrección
$ul_count_before = substr_count($html, ' antes: $ul_count_before\n";
echo "Tags después: $ul_count_after\n";
$li_count_before = substr_count($html, '- antes: $li_count_before\n";
echo "Tags
- después: $li_count_after\n";
echo "\n========================================\n";
echo "NOTA: Este patrón elimina el
prematuro\n";
echo "pero NO agrega el
faltante al final.\n";
echo "Se necesita un segundo paso para balancear.\n";
echo "========================================\n";
$conn->close();