Problems displaying French accented characters in UTF-8

前端 未结 4 1827
清酒与你
清酒与你 2021-01-12 07:14

I\'m working on a French language site built in CakePHP. I have tried multiple functions to try and convert the text into UTF-8 and display properly, but have had no succes

相关标签:
4条回答
  • 2021-01-12 07:31

    Check your @@character_set_results. By default, MySQL uses latin1, not utf8. Try SET NAMES utf8 or mysqli::set_charset.

    Update: here is how you might check the character sets in use:

    mysql> SHOW VARIABLES LIKE '%char%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       | 
    | character_set_connection | utf8                       | 
    | character_set_database   | utf8                       | 
    | character_set_filesystem | binary                     | 
    | character_set_results    | utf8                       | 
    | character_set_server     | utf8                       | 
    | character_set_system     | utf8                       | 
    | character_sets_dir       | /usr/share/mysql/charsets/ | 
    +--------------------------+----------------------------+
    

    Read more on dev.mysql.com.

    0 讨论(0)
  • 2021-01-12 07:31

    I just fixed a very similar problem with accented characters not displaying. My database table is utf-8 encoded and my html header is correctly set for utf-8.

    I hadn't specified a charset for my MySQL connection, in my case I'm using PDO.

    $this->pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8;", $username, $password);
    

    No problems now!

    0 讨论(0)
  • 2021-01-12 07:31

    In case this helps anyone using PHP Classes.

    My solution was to set Character Encoding to my Connection.

    //connect to MySQL and select DB
    public function open_connection() {
        $this->connection = new mysqli( DB_SERVER, DB_USER, DB_PASS, DB_NAME );
        if ( !$this->connection ) {
            die( "Database connection failed: " . mysqli_error() );
        }
        mysqli_set_charset($this->connection,"utf8");
    }
    
    0 讨论(0)
  • 2021-01-12 07:44

    First: Check your php files encoding! I work on Mac, I use Coda to program, and it has an option to convert charset's, sometimes i get troubles like this and converting to UTF-8 always fix them. I think that Notepad++ can do that on windows. (If you do this on your PHP files, every strings on them will not need the functions htmlspecialchars(), html_entity_decode, etc )

    Second: if you are using HTML Output, check if you have <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> on your header...

    Third: Do what @janmoesen said on your MySQL DB.

    Tell me something about that.

    0 讨论(0)
提交回复
热议问题