Strange specifications on the HTML page

I use the Last.fm API to get information about artists. I save the information in the database and then display it on my web page. But characters like "(double quotation mark) are shown as".

Example Artist Information http://www.last.fm/music/David+Penn

and I got the first line as "Producer, arranger, DJ and musician from Madrid-Spain. He has his own record company, Zen Records and."

Mine Db is UTF-8, but I don’t know why this error still occurs.

0
source share
2 answers

You must use UTF-8 to the end. Make sure that:

  • your database connection is UTF-8 (using mysql_set_charset );

  • output pages are marked as UTF-8 ( <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> );

  • when you output rows from a database, you encode their HTML with htmlspecialchars() , not htmlentities() .

htmlentities HTML encodes all non-ASCII characters, and by default assumes that you pass bytes to ISO-8859-1. Therefore, if you pass it " encoded as UTF-8 (bytes 0xE2, 0x80, 0x9C), you will get &acirc;&#128;&#156; instead of the expected &ldquo; or &#8220; This can be fixed by passing utf-8 as an optional argument to $charset .

However, it is usually simpler to just use htmlspecialchars() , as this leaves only non-ASCII characters as source bytes instead of references to HTML objects. This results in a smaller page release, so it is preferable if you are sure that the HTML you create will contain encoding information (which you can usually rely on, except in context, such as sending HTML fragments by mail or something else).

htmlspecialchars() has an optional $charset argument, but setting it to utf-8 not critical, since it does not change the default behavior in ISO-8859-1 encoding. If you are generating output in old-school multibyte encodings such as Shift-JIS, you have to worry about setting this argument correctly, but today it is pretty rare, as most sane people prefer UTF-8.

+1
source

This seems to be a character encoding error. Make sure you read the webpage as the correct encoding and show the results in the correct encoding.

+2
source

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


All Articles