Como creo una url y un slug amigable?
Publicado por albert (21 intervenciones) el 11/09/2021 18:44:01
hola necesito codigo para generar una url y luego redicionarlo con una url amigable - alguna pagina web?
Valora esta pregunta


0
function url_amigable($url, $pisoBajo = true)
{
$url = strtolower(trim($url));
$n = array('Ñ', 'ñ');
$url = str_replace($n, 'n', $url);
$find = array(' ', '&', '\r\n', '\n', '+', ',');
$url = str_replace($find, '-', $url);
$find = array('/[^a-z0-9\-<>]/', '/[\-]+/', '/<[^>]*>/');
if ($pisoBajo) $repl = array('', '_', '');
else $repl = array('', '-', '');
$url = preg_replace($find, $repl, $url);
return $url;
}
<?php
if (isset($_GET['hash'])) {
$nombre=implode('!', array_slice(explode('!', base64_decode($_GET['hash'])), 1, 1));
$insert = "INSERT INTO tabla(nombre)VALUES('" . $nombre . "')";
echo $insert;
} else {
function maskGet($val, $len1, $len2,$char)
{
$cod = 'abc1357902468pqrstvwxyzABCD';
$res = '';
for ($i = 1; $i <= $len1; $i++) {
$res .= substr($cod, (rand() % (strlen($cod))), 1);
}
$key = '';
$code = 'abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ';
$max = strlen($code) - 1;
for ($i = 0; $i < $len2; $i++)
$key .= $code{mt_rand(0, $max)};
return base64_encode($res . $char . $val . $char . $key);
}
echo '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GET</title>
</head>
<body>
<h3>Supongamos deseas enviar el nombre <b>JUAN</b> por get</h3>
<span>En el input name ya coloco el nombre enmascarado para no estar haciendo mas peticiones (creo que se puede entender la idea)</span><br><br>
<form method="get" action="' . $_SERVER['PHP_SELF'] . '">
Nombre: <input type="text" name="hash" value="' . maskGet('JUAN', 12, 12,'!') . '" style="width:500px" readonly/><br><br>
<button type="submit">Envia</button>
</form>
</body>
</html>
';
} ?>