How to prevent certain characters from being entered in a field

0

I'm with a form, where you have to enter a place of an event. It occurred to me to put in place of the word "Barrio" The characters "B °" and the recording process gave me an error.

$lugar = htmlentities(addslashes($_POST['lugar']));

thus "clean" and control what is entered in that field. I use PDO to make the data recording in MySQL

$stmt->bindValue(":lugar", $lugar);

This process the information of the field in question What may be happening?

    
asked by MNibor 29.08.2017 в 03:35
source

1 answer

1

this code can help you to normalize the string:

function sanear_string($string)
{

$string = trim($string);

$string = str_replace(
    array('á', 'à', 'ä', 'â', 'ª', 'Á', 'À', 'Â', 'Ä'),
    array('a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A'),
    $string
);

$string = str_replace(
    array('é', 'è', 'ë', 'ê', 'É', 'È', 'Ê', 'Ë'),
    array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E'),
    $string
);

$string = str_replace(
    array('í', 'ì', 'ï', 'î', 'Í', 'Ì', 'Ï', 'Î'),
    array('i', 'i', 'i', 'i', 'I', 'I', 'I', 'I'),
    $string
);

$string = str_replace(
    array('ó', 'ò', 'ö', 'ô', 'Ó', 'Ò', 'Ö', 'Ô'),
    array('o', 'o', 'o', 'o', 'O', 'O', 'O', 'O'),
    $string
);

$string = str_replace(
    array('ú', 'ù', 'ü', 'û', 'Ú', 'Ù', 'Û', 'Ü'),
    array('u', 'u', 'u', 'u', 'U', 'U', 'U', 'U'),
    $string
);

$string = str_replace(
    array('ñ', 'Ñ', 'ç', 'Ç'),
    array('n', 'N', 'c', 'C',),
    $string
);

$string = str_replace(
    array('¨', 'º', '-', '~',
         '#', '@', '|', '!', '"',
         '·', '$', "%", "&", "/",
         "(", ")", "?", "'", "¡",
         "¿", "[", "^", "<code>", "]",
         "+", "}", "{", "¨", "´",".",
         ">", "< ", ";", ",", ":"),
    '',
    $string
);
return $string;
}

Courtesy of: link

Likewise PHP already has methods by defects:

link

link

    
answered by 02.09.2017 / 01:43
source