1
<?php
$url = "https://academia.kilopolis.com:8443/cursos/backend-php?orden=reciente#temario";
// Patrón con grupos nombrados semánticos
$patron = '#^(?P<protocolo>https?)://(?P<host>[a-z0-9.-]+)(?::(?P<puerto>\d+))?(?P<ruta>/[^\s?#]*)?(?:\?(?P<query>[^\s#]*))?(?:#(?P<fragmento>[^\s]*))?$#i';
echo "<div style='font-family:system-ui; max-width:640px; padding:15px;'>";
echo "<h3 style='color:#0f172a; margin-top:0;'>🌐 Parser Semántico de URLs con (?P<id>)</h3>";
echo "<p style='color:#64748b; font-size:13px;'>URL analizada: <code style='word-break:break-all;'>" . htmlspecialchars($url) . "</code></p>";
if (preg_match($patron, $url, $datos)) {
echo "<div style='background:#f8fafc; border:1px solid #cbd5e1; border-radius:8px; padding:15px;'>";
echo " <h4 style='margin:0 0 10px 0; color:#1e293b; font-size:15px;'>Componentes Identificados:</h4>";
$campos = [
'protocolo' => 'Protocolo Web',
'host' => 'Servidor / Host',
'puerto' => 'Puerto de Red (Opcional)',
'ruta' => 'Ruta del Endpoint',
'query' => 'Parámetros Query String',
'fragmento' => 'Ancla / Hash Fragment'
];
foreach ($campos as $clave => $etiqueta) {
$valor = !empty($datos[$clave]) ? $datos[$clave] : '<em style="color:#94a3b8;">(no especificado)</em>';
echo "<div style='display:flex; justify-content:space-between; padding:6px 0; border-bottom:1px solid #f1f5f9;'>";
echo " <span style='color:#64748b; font-size:13px;'>$etiqueta:</span>";
echo " <strong style='font-family:monospace; color:#0284c7;'>$valor</strong>";
echo "</div>";
}
echo "</div>";
} else {
echo "<p style='color:#dc2626;'>URL no válida.</p>";
}
echo "</div>";
Haz clic para editar este código