Convert special characters using htmlspecialchars and htmlentities

I got confused about entering special characters into my database now ...

For example, I want to accept characters like ö , ù , etc., and I want to display them as they are in html, for example Löic , which is a French name.

And I thought I should convert these special characters to html objects before entering them into a database like ö for ö .

If I use htmlspecialchars() to convert them,

  Array ( [name] => Löic ) if(isset($_POST['name'])) $name = preg_replace('/\s\s+/', ' ', trim($_POST['name'])); $name = htmlspecialchars($name, ENT_QUOTES); 

Therefore, it must be converted to this Löic , but it is not converted at all, and not as it is,

 Löic 

So this is what I get when I check my database, it is saved as

 Löic 

If I use htmlentities() to convert them,

 $name = htmlentities($name, ENT_QUOTES); 

then I get it

 Löic 

and it displays like this on my html,

 Löic 

Why is this so? My head will explode! I do not know where the problem is. Please help ... What should I do to do it right?

Is this related to utf8_general_ci , which I set in the name column?

Thanks.

+2
source share
2 answers

This is because you are trying to convert utf8 characters with htmlentites. Unfortunately, the function only works on ascii characters. The simplest solution is to simply store your rows in your utf8 database table and only output them using htmlspecialchars instead of htmlentities. Just remember to set the page encoding to utf8.

+1
source

Also you can simply:

 $text = htmlspecialchars( $text ); $text = mysql_escape_string( $text ); 
+1
source

Source: https://habr.com/ru/post/886738/


All Articles