-1

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 like this:

acatlán de juárez

And the process repeats back and forth. This is not something that should be happening in my application because I'm comparing many things with this words.

I already have utf-8 on my header

<meta charset="UTF-8">

The database collation used: utf8_unicode_ci

The variable causing this errors is assigned like this:

$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);

// $theString now is $theString = "Acatlán de Juárez"

So $theString variable, when echoed or doing a query, sometimes prints "acatl��n de ju��rez" and some other times prints "acatlán de juárez".


EDIT

I'm starting to receive alot of answers regarding database queries, it actually has nothing to do with the Database, accents are correctly ouputed from the database. Problem is with the variable $theString explained in the code above, it is not printed like it should, with accents.


EDIT 2

I don't know what's happening now, I'm really confused, in this example:

$a = array(
'animal' => array('acatlán de juárez','hów','áre'),
'color'  => array('yóu','good','thanks')
);
echo $a['animal'][0];

My code outputs the word "acatlán de juárez" perfectly.

But the I have my array $cities (exactly like my array $a) but with states and cities. Like this:

$cities = array(
'state1' => array('acatlán de juárez','city1','city2'),
'state2'  => array('city1','city2','city3')
);
echo $cities['state1'][0];

And now "acatlán de juárez" becomes "acatl��n de ju��rez".... WHY?!?


EDIT 3

Question solved, it was another function bugging the charset format, thank you everyone.

Daniel
  • 378
  • 1
  • 6
  • 20
  • What database are you using, if any? Is this PHP or HTML output? Can we see the code that produces this? What encoding are you using for your files? – halfer Mar 01 '15 at 18:28
  • @halfer I assign an accented word to a variable, say $words = "Acatlán de Juárez"; Then I send this variable to my HTML document and I do `echo $words;`. Then I get the result in my description explained above, thanks EDIT: btw, I'm using MySQL – Daniel Mar 01 '15 at 18:31
  • @halfer Also, before sending it to my HTML document I do a query that when I get "acatl��n de ju��rez" it doesn't find the result in my database, but sometimes it does when is "acatlán de juárez".... This is a huge issue for me right now. Thanks – Daniel Mar 01 '15 at 18:34
  • What collation are you using in your database? Does it always look correct in phpMyAdmin, or a unicode MySQL console? What operating system are you using? (Historically Windows has not had a good console, so use phpMyAdmin or MySQL Workbench if you are on this operating system). – halfer Mar 01 '15 at 18:39
  • Repeating my questions again (these will require question edits please): 1. Can we see the code that produces this?, 2. What encoding are you using for your files? (this is a setting in your IDE or editor, e.g. NetBeans). – halfer Mar 01 '15 at 18:42
  • @halfer Collation is `utf8_unicode_ci`, it looks correct in PhpMyAdmin, I'm using Windows 8... Accents are printed perfectly on all pages, just not this way. I'm using the code suggested in the answer for this question: http://stackoverflow.com/questions/28795995/php-replace-string-found-in-array-with-another-in-other-array And the result is the variable I have that is echoing the accents like this "acatl��n de ju��rez" and sometimes like this "acatlán de juárez" – Daniel Mar 01 '15 at 18:42
  • @halfer added more details – Daniel Mar 01 '15 at 18:46
  • @halfer is just an small snippet, I actually have huge array of states and cities of Mexico, so since URL's don't work well with accents I'm doing it this way – Daniel Mar 01 '15 at 18:49
  • [This might be a duplicate](http://stackoverflow.com/questions/25465114/php-str-replace-not-working-with-special-chars)? – halfer Mar 01 '15 at 18:51
  • No way, that question is from a year ago -.- – Daniel Mar 01 '15 at 18:52
  • @Daniel can you post a sample code using MySQL of how you're retrieving your data? **Also are you using AJAX/JQuery to retrieve any of that MySQL data?** – Prix Mar 01 '15 at 19:20
  • @Prix I'm using Laravel as my framework. But I believe it's not the relevant part, anyway here it is: `DB::table('cars')->join('users', 'cars.by_user_id', '=', 'users.user_id')->where('users.state','=',$state)->where('users.city','=',$city)->orderBy('package', 'desc')->get();` The problem has to do with the variable I believe, because with other data the accents are printed correctly when outputed from the database – Daniel Mar 01 '15 at 19:24
  • Please see my answer [here](http://stackoverflow.com/questions/28790757/cannot-display-trademark-symbol-in-mysql-to-html/28792339). – EternalHour Mar 01 '15 at 20:01
  • @Daniel it is relevant because we don't know if you're setting any charset in your db connection nor do we know your mysql server is running as and your issue is directly relevant to the data you're reading from your database. Fixing a different issue may not always fix the actual issue. – Prix Mar 01 '15 at 21:36
  • (Pleased you sorted it out. It's worth also bearing in mind that the age of another question has no bearing on whether it is a duplicate or not). – halfer Mar 01 '15 at 23:37

3 Answers3

2

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

CREATE TABLE `quote` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` char(4) COLLATE utf8mb4_unicode_ci NOT NULL',
  `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';
Community
  • 1
  • 1
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • Thanks alot for your answer, but this is not my issue, I print the result even before from doing the query and I still get the iconrrect accents, please see the last snippet in my description, if I echo $theString I get `"acatl��n de ju��rez"` but if I echo just `"acatlán de juárez"` I get the expected result. Everything happens when converting the array to the other – Daniel Mar 01 '15 at 20:05
  • Have tried `accept-charset="ISO-8859-1" in meta instead of utf8 or `mb_internal_encoding("utf-8"); or `utf8_decode()` or `iconv()` so on. In your php code? Try and see what happens. – BentCoder Mar 01 '15 at 20:40
1

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>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
    <?php echo $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

  • Thanks alot for your answer, but this is not my issue, I print the result even before from doing the query and I still get the iconrrect accents, please see the last snippet in my description, if I echo $theString I get `"acatl��n de ju��rez" ` but if I echo just "Acatlán de Juárez" I get the expected result. Everything happens when converting the array to the other – Daniel Mar 01 '15 at 19:43
  • I've updated the code with your example. Its working. Always prints out: acatlán de juárez – user3790692 Mar 01 '15 at 20:08
  • Thanks alot, I really apology you are correct 100% but it's not solving my issue please take a look at my "EDIT 2" in description. I'm really confused :( – Daniel Mar 01 '15 at 20:18
0

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.

Daniel
  • 378
  • 1
  • 6
  • 20
  • In general we don't permit people to delete questions here, because a solution may be useful to someone else in the future. For the same reason, can you expand a bit more on how the "charset format" was lost? – halfer Mar 01 '15 at 23:36