Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Characters not displaying correctly on a UTF-8 website

I've done everything I can think of, but special characters are not displaying correctly on this webpage.

For example, in the database it's:

enter image description here

But on the site it's:

Nouveaux R�alistes

Here's everything I've checked...

The database is set to UTF-8:

enter image description here

The page was written in NetBeans, with the document encoding set to UTF-8:

enter image description here

The page header declares UTF-8:

enter image description here

The meta charset is set to UTF-8:

enter image description here

I've even added the following line to my .htacess:

enter image description here

But there characters are still not displaying correctly, and I get the following error from the W3C Validator:

enter image description here

I feel like I've attempted everything, but it still won't work. (I've even tried htmlspecialchars and htmlentities in PHP, but the page doesn't even render!)


UPDATE
As requested, here is the code I'm using:

class Exhibition {
    public $exhibitionDetails;    

    public function __construct(Database $db, $exhibitionID){
        $this->_db = $db;

        $params['ExhibitionID'] = $exhibitionID;

        $STH = $this->_db->prepare("SELECT * 
            FROM Exhibition
            INNER JOIN Schedule
                ON Exhibition.ExhibitionID = Schedule.ExhibitionID            
            WHERE Schedule.Visible = 1
                AND Exhibition.ExhibitionID = :ExhibitionID;");

        $STH->execute($params);

        $this->exhibitionDetails = $STH->fetchAll(PDO::FETCH_ASSOC);

    }
}

And...

try {
    $db = new Database(SITE_ROOT."/../");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $exhibition = new Exhibition($db,$_GET['id']);
} catch (PDOException $e) {
    echo "<p class='error'>ERROR: ".$e->getMessage()."</p>";
}

And finally...

<p><?php echo $exhibition->exhibitionDetails[0]["Desc"]; ?></p>
like image 558
Chuck Le Butt Avatar asked Apr 18 '13 20:04

Chuck Le Butt


People also ask

What characters are not allowed in UTF-8?

Yes. 0xC0, 0xC1, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF are invalid UTF-8 code units.

Can UTF-8 handle all characters?

UTF-8 extends the ASCII character set to use 8-bit code points, which allows for up to 256 different characters. This means that UTF-8 can represent all of the printable ASCII characters, as well as the non-printable characters.

How do I change my UTF-8 character set?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.

Can URLs have UTF-8 characters?

Building a valid URL By the same token, any code that generates or accepts UTF-8 input might treat URLs with UTF-8 characters as "valid", but would also need to translate those characters before sending them out to a web server. This process is called URL-encoding or percent-encoding.


2 Answers

If you are using mysql_* functions:

mysql_query("SET NAMES 'utf8'");

If you are using PDO

$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);

It sets connection encoding.

like image 72
Wesley Schleumer de Góes Avatar answered Sep 22 '22 14:09

Wesley Schleumer de Góes


It's been a few years since I've used PHP but back then it didn't natively support Unicode and a quick search of google tells me it still doesn't. You can still make it work though.

Here's a great link:

Encodings And Character Sets To Work With Text

like image 24
Captain Skyhawk Avatar answered Sep 23 '22 14:09

Captain Skyhawk