John Davidson

php - Content-Type charset won't change

0 comments
Message:


I'm trying to change my website from windows-1251 to utf-8, but it won't budge.


There are a ton of "solutions" to this on web, but any doesn't seem to be sufficient.


Here is what I did:



  1. I changed the encoding of the index.html (smarty template) and index.php files to UTF-8.



  2. I set


     <html lang="ru">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">



in index.html



  1. I set


    header('Content-type: text/html; charset=utf-8');



in several places in index.php



  1. I changed


    default_charset="UTF-8"



in php.ini and restarted my Apache(XAMPP) server


And still, the Chrome devtools say my page is "Content-Type: text/html; charset=windows-1251" and I see gibberish instead of Cyrillic in my browser.


What else can I do?


P.S. my index.html looks like this now:


<?php header('Content-type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>

<html lang="ru">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Тест
</body>
</html>

and I open it dirrectly via the browser: http://localhost:8080/index.html



My Answer:
In PHP, you can set the Content-Type header along with the charset using the header() function. Here's an example of how you can set the Content-Type to text/html with UTF-8 charset:

php
header('Content-Type: text/html; charset=utf-8');


This will ensure that the browser knows how to interpret the content of the response correctly. Make sure to set the Content-Type header before any output is sent to the browser.

Rate this post

5 of 5 based on 6439 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us