{"id":3878,"date":"2022-08-20T21:39:46","date_gmt":"2022-08-20T16:09:46","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/"},"modified":"2022-08-20T21:39:46","modified_gmt":"2022-08-20T16:09:46","slug":"solved-utf-8-all-the-way-through","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/","title":{"rendered":"(Solved) UTF-8 all the way through"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-279279\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"279279\" data-parentid=\"279170\" data-score=\"1105\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><strong>Data Storage<\/strong>:<\/p>\n<ul>\n<li>\n<p>Specify the <code>utf8mb4<\/code> character set on all tables and text columns in your database.  This makes MySQL physically store and retrieve values encoded natively in UTF-8.  Note that MySQL will implicitly use <code>utf8mb4<\/code> encoding if a <code>utf8mb4_*<\/code> collation is specified (without any explicit character set).<\/p>\n<\/li>\n<li>\n<p>In older versions of MySQL (&lt; 5.5.3), you&#8217;ll unfortunately be forced to use simply <code>utf8<\/code>, which only supports a subset of Unicode characters.  I wish I were kidding.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Data Access<\/strong>:<\/p>\n<ul>\n<li>\n<p>In your application code (e.g. PHP), in whatever DB access method you use, you&#8217;ll need to set the connection charset to <code>utf8mb4<\/code>.  This way, MySQL does no conversion from its native UTF-8 when it hands data off to your application and vice versa.<\/p>\n<\/li>\n<li>\n<p>Some drivers provide their own mechanism for configuring the connection character set, which both updates its own internal state and informs MySQL of the encoding to be used on the connection\u2014this is usually the preferred approach.   In PHP:<\/p>\n<ul>\n<li>\n<p>If you&#8217;re using the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/book.pdo.php\">PDO<\/a> abstraction layer with PHP \u2265 5.3.6, you can specify <code>charset<\/code> in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/ref.pdo-mysql.connection.php\">DSN<\/a>:<\/p>\n<pre><code> $dbh = new PDO('mysql:charset=utf8mb4');\n<\/code><\/pre>\n<\/li>\n<li>\n<p>If you&#8217;re using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/book.mysqli.php\">mysqli<\/a>, you can call <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/mysqli.set-charset.php\"><code>set_charset()<\/code><\/a>:<\/p>\n<pre><code>  $mysqli-&gt;set_charset('utf8mb4');       \/\/ object oriented style\n  mysqli_set_charset($link, 'utf8mb4');  \/\/ procedural style\n<\/code><\/pre>\n<\/li>\n<li>\n<p>If you&#8217;re stuck with plain <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/book.mysql.php\">mysql<\/a> but happen to be running PHP \u2265 5.2.3, you can call <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.mysql-set-charset.php\"><code>mysql_set_charset<\/code><\/a>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/dev.mysql.com\/doc\/en\/charset-connection.html\"><code>SET NAMES 'utf8mb4'<\/code><\/a>.<\/p>\n<\/li>\n<li>\n<p>The same consideration regarding <code>utf8mb4<\/code>\/<code>utf8<\/code> applies as above.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Output<\/strong>:<\/p>\n<ul>\n<li>UTF-8 should be set in the HTTP header, such as <code>Content-Type: text\/html; charset=utf-8<\/code>. You can achieve that either by setting <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/ini.core.php#ini.default-charset\"><code>default_charset<\/code><\/a> in php.ini (preferred), or manually using <code>header()<\/code> function.<\/li>\n<li>If your application transmits text to other systems, they will also need to be informed of the character encoding.  With web applications, the browser must be informed of the encoding in which data is sent (through HTTP response headers or HTML metadata).<\/li>\n<li>When encoding the output using <code>json_encode()<\/code>, add <code>JSON_UNESCAPED_UNICODE<\/code> as a second parameter.<\/li>\n<\/ul>\n<p><strong>Input<\/strong>:<\/p>\n<ul>\n<li>Browsers will submit data in the character set specified for the document, hence nothing particular has to be done on the input.<\/li>\n<li>In case you have doubts about request encoding (in case it could be tampered with), you may verify every received string as being valid UTF-8 before you try to store it or use it anywhere.  PHP&#8217;s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.mb-check-encoding.php\"><code>mb_check_encoding()<\/code><\/a> does the trick, but you have to use it religiously.  There&#8217;s really no way around this, as malicious clients can submit data in whatever encoding they want, and I haven&#8217;t found a trick to get PHP to do this for you reliably.<\/li>\n<\/ul>\n<p><strong>Other Code Considerations<\/strong>:<\/p>\n<ul>\n<li>\n<p>Obviously enough, all files you&#8217;ll be serving (PHP, HTML, JavaScript, etc.) should be encoded in valid UTF-8.<\/p>\n<\/li>\n<li>\n<p>You need to make sure that every time you process a UTF-8 string, you do so safely.  This is, unfortunately, the hard part.  You&#8217;ll probably want to make extensive use of PHP&#8217;s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/book.mbstring.php\"><code>mbstring<\/code><\/a> extension.<\/p>\n<\/li>\n<li>\n<p><strong>PHP&#8217;s built-in string operations are <em>not<\/em> by default UTF-8 safe.<\/strong>  There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent <code>mbstring<\/code> function.<\/p>\n<\/li>\n<li>\n<p>To know what you&#8217;re doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level.  Check out any of the links from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.utf8.com\/\">utf8.com<\/a> for some good resources to learn everything you need to know.<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved UTF-8 all the way through <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Data Storage: Specify the utf8mb4 character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set). In older versions of MySQL (&lt; 5.5.3), &#8230; <a title=\"(Solved) UTF-8 all the way through\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\" aria-label=\"More on (Solved) UTF-8 all the way through\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[425,424,340,339,426],"class_list":["post-3878","post","type-post","status-publish","format-standard","hentry","category-solved","tag-apache","tag-linux","tag-mysql","tag-php","tag-utf-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) UTF-8 all the way through - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) UTF-8 all the way through - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Data Storage: Specify the utf8mb4 character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set). In older versions of MySQL (&lt; 5.5.3), ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T16:09:46+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) UTF-8 all the way through\",\"datePublished\":\"2022-08-20T16:09:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\"},\"wordCount\":573,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"apache\",\"linux\",\"mysql\",\"php\",\"utf-8\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\",\"name\":\"(Solved) UTF-8 all the way through - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T16:09:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) UTF-8 all the way through\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) UTF-8 all the way through - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) UTF-8 all the way through - JassWeb","og_description":"[ad_1] Data Storage: Specify the utf8mb4 character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set). In older versions of MySQL (&lt; 5.5.3), ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T16:09:46+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) UTF-8 all the way through","datePublished":"2022-08-20T16:09:46+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/"},"wordCount":573,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["apache","linux","mysql","php","utf-8"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/","url":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/","name":"(Solved) UTF-8 all the way through - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T16:09:46+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-utf-8-all-the-way-through\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) UTF-8 all the way through"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3878"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3878\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}