简体   繁体   中英

PHP htmlentities or htmlspecialchars = acentuation error

I have this function on CONFIG.PHP:

foreach($_POST as $key => $value){ 
  if (!is_array($value)){
  $_POST[$key] = htmlspecialchars($value);
  }
}

foreach($_GET as $key => $value){ 
  if (!is_array($value)){
  $_GET[$key] = htmlentities($value);
  }
}

To prevent html injection in all POSTs and GETs. It gets value from post, get, htmlentities it and record in mysql.

my mysql collation: utf8mb4_general_ci

the problem is, without htmlentities, acentuation is fine, I can read everthing on mysql. but with htmlentities words with acentuation become crazy like that:

não

é

what i am doing wrong?

htmlspecialchars and htmlentities are ment to exchange special charakters with their code-version. html would still show the symbol as it translates the codes automaticly. But you remove the symbols themselves. Thus your result is excatly what you should get.

If you don't want he code-version of the symbosl, fe if you want to search in them later on, you should escape all the variables instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM