Ver Mensaje Individual
  #8 (permalink)  
Antiguo 20/11/2012, 10:45
Avatar de bng5
bng5
 
Fecha de Ingreso: junio-2009
Ubicación: 127.0.0.1
Mensajes: 269
Antigüedad: 14 años, 11 meses
Puntos: 24
Respuesta: Acentos en post no aceptados en preg_match

Te paso un ejemplo de cuando el juego de caracteres te puede complicar en esta tarea.

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="es-uy" lang="es-uy">
<head>
 <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
 <!-- meta http-equiv="content-type" content="text/html;charset=utf-8" / -->
 <title>Charset</title>
</head>
<body>

<form method="post" action="">
    <input type="text" name="categoria" value="" />
    <input type="submit" value="enviar" />
</form>

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
$categoria $_POST['categoria'];
    if(
preg_match("/^[a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ ]+$/"$categoria)) {
        echo 
'Caracteres válidos';
    }
    else {
        echo 
'Hay caracteres no válidos';
    }
}

?>

</body>
</html>
Si guardas este archivo con una codificación UTF-8 y le envías a través del form la cadena 'áá', el navegador la enviará en ISO-8859-1 (porque es lo que indica el tag meta) y dirá:

Cita:
Hay caracteres no válidos
en cambio, si este mismo archivo lo guardas en ISO-8859-1 y envías el mismo string a través del mismo form, dirá:

Cita:
Caracteres válidos