{"id":102,"date":"2011-12-31T17:17:17","date_gmt":"2011-12-31T17:17:17","guid":{"rendered":"http:\/\/boulderinformationservices.wordpress.com\/?p=102"},"modified":"2011-12-31T17:17:17","modified_gmt":"2011-12-31T17:17:17","slug":"utf-8-not-working-look-for-htmlentities","status":"publish","type":"post","link":"https:\/\/boulderinformationservices.com\/blog\/utf-8-not-working-look-for-htmlentities\/","title":{"rendered":"UTF-8 not working? Look for htmlentities."},"content":{"rendered":"<p>I converted all my sites to UTF-8 and it was relatively painless. However, on one of my sites, I kept seeing legal UTF-8 characters turning into gibberish. It happened after entering UTF-8, submitting a form and then redisplaying the values in an edit box.<\/p>\n<p><a href=\"http:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8-before.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-103\" title=\"utf8-before\" src=\"http:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8-before.png?w=300\" alt=\"\" width=\"300\" height=\"66\" srcset=\"https:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8-before.png 398w, https:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8-before-300x66.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-104\" title=\"utf8\" src=\"http:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8.png?w=300\" alt=\"\" width=\"300\" height=\"69\" srcset=\"https:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8.png 427w, https:\/\/boulderinformationservices.com\/blog\/wp-content\/uploads\/2011\/12\/utf8-300x70.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I know that all my pages are using UTF-8, and also that my database stores the text as UTF-8.<\/p>\n<p>The thing that I overlooked is that, because I&#8217;m writing text to an input box, I&#8217;m using PHP&#8217;s htmlentities function to handle special characters, and the default character set used by that function is ISO-8859-1. I had to change my PHP command from<\/p>\n<pre>echo htmlentities($field);<\/pre>\n<p>to<\/p>\n<pre>echo htmlentities($field, ENT_COMPAT, 'UTF-8');<\/pre>\n<p>Once I verified that worked, I searched my whole codebase for htmlentities and made sure they were all using &#8216;UTF-8&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I converted all my sites to UTF-8 and it was relatively painless. However, on one of my sites, I kept seeing legal UTF-8 characters turning into gibberish. It happened after entering UTF-8, submitting a form and then redisplaying the values &hellip; <a href=\"https:\/\/boulderinformationservices.com\/blog\/utf-8-not-working-look-for-htmlentities\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,4,16],"tags":[],"_links":{"self":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":0,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}