Skip to content Skip to sidebar Skip to footer

Accents Printed Differently Every Time, In Php And Html

I'm echoing into my page some accented words, that sometimes are printed like this: acatl��n de ju��rez And after refreshing the page a few times again, it prints them lik

Solution 1:

I had exactly the same issue as yours a few weeks ago and the solved the problem after modifying database COLLATE and CHARSET as well as how I handle MySQL queries. My post is here.

  1. Set you DB's "default collection" to utf8mb4 - utf8mb4_unicode_ci
  2. Make sure "COLLATE" property of table fields are set to utf8mb4_unicode_ci as well.
  3. Set "meta" tag to <meta charset="utf-8"> in your HTML.
  4. When you do SELECT, INSERT, UPDATE on MySQL, call $mysqli->set_charset('utf8mb4');

Note: I suggest you to read up on why utf8mb4 or why not utf8 in general. Also read this.

EXAMPLE TABLE

CREATETABLE `quote` (
  `id` int(10) unsigned NOTNULL AUTO_INCREMENT,
  `code` char(4) COLLATE utf8mb4_unicode_ci NOTNULL',
  `quote` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

EXAMPLE SELECT

Note: Pay attention to set_charset()

$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT * FROM quote');
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
   //Do something with $row
}

EXAMPLE INSERT/UPDATE

$mysqli->set_charset('utf8mb4');
$sql = "INSERT INTO quote (code, quote) VALUES (?, ?)";
//$sql = "UPDATE ......";$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ss', $_POST['code'], $_POST['quote']);
if (! $stmt->execute()) {
    echo$stmt->error;
}
echo'Nice one';

Solution 2:

Its a charset error.

This is a simple model:

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

$theString = "acatlan de juarez";

$a = array(
    'animal' => array('acatlán de juárez','hów','áre'),
    'color'  => array('yóu','good','thanks')
);
$b = array(
    'animal' => array('acatlan de juarez','how','are'),
    'color'  => array('you','good','thanks')
);
$theString = str_replace($b['animal'], $a['animal'], $theString);
?><!DOCTYPE HTML><htmllang="en"><head><metacharset="UTF-8"><title></title></head><body><?phpecho$theString; ?></body></html>

Run the Example

And for the DB if you are using MySQL or MySQLi:

//MYSQL
mysql_connect('host','usuer','pass');
mysql_select_db('database');
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

//MYSQLi$mysqli = new mysqli('host', 'user', 'pass', 'database');
$mysqli->set_charset("utf8");

Of corse that you have to conf your db to use: utf8_unicode_ci

Solution 3:

This thread was solved. There was another function causing the string to loose charset format. I'm not able to delete this thread, but thank you everyone for your help. You were right all the time.

Post a Comment for "Accents Printed Differently Every Time, In Php And Html"