2

I'm trying to print data from an array:

$card_list = Array(Array("Red", "あかい"), Array("Blue", "あおい"), Array("Green", "みどり"));
foreach ($card_list as $card_row)
{
  print $card_row[0] . " | " . $card_row[1] . "<br />";
}

However, chrome and IE. both output:

Red | ???
Blue | ??
Green | ???

Expected output is:

Red | あかい
Blue | あおい
Green | みどり

I've tried adding the following things to make it work (from past answers to similar questions) to no avail.

header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8'); 
<meta charset="utf-8" />

Is it possible to get this to print without escaping the input? I'd really rather not having to do that.

As requested, here's all related code that I'm running with at the moment

<?php

  header('Content-Type: text/html; charset=UTF-8');
  mb_internal_encoding('UTF-8'); 
  $card_list = Array(Array("Red", "あかい"), Array("Blue", "青い"), Array("Green", "みどり"));

?>
<html lang="ja">
  <head>  
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="flashcard_block">
    <?php
      foreach ($card_list as $card_row)
      {
          print $card_row[0] . " | " . $card_row[1] . "<br />";
      }
    ?>
    </div>
  </body>
</html>
Jamus
  • 865
  • 4
  • 11
  • 28
  • Rewrite the current `` tag to: `` and that's being properly placed within the ``? – MackieeE Mar 05 '14 at 09:15
  • @MackieeE I had that originally, I read somewhere that it was outdated.. made no difference, but thanks anyway :) – Jamus Mar 05 '14 at 09:17
  • Alright =) I only ask that if it's being properly placed (As how it's currently written on the question, it ***wouldn't*** work), as that would be quite important, could you show the rest of your header html? – MackieeE Mar 05 '14 at 09:21
  • @MackieeE haha yes, it's not mixed in with PHP in the real one :P I updated the question with an example of what I'm using – Jamus Mar 05 '14 at 09:26
  • thanks for editing! :) I've tested that same snippet and it works with Chrome, must be something simple or deeper! x) Would be interesting to find out what `mb_detect_encoding()` returns for `$card_row[1]` – MackieeE Mar 05 '14 at 09:37
  • You definitely don't need `mb_internal_encoding()` and what you have posted should work just peachy. Unless perhaps your php script itself is encoded in some funky character set :) – Ja͢ck Mar 05 '14 at 13:21
  • Sorry @MackieeE ! For some reason updates to this page stopped showing. 'mb_detect_encoding()' returns ASCII; I'm not sure if that's what should be showing up to be honest, but I get the same result when copy/pasting text from Wikipedia. I've tested this in Chrome and IE on my system, including the raw kernals for both without any success. – Jamus Mar 06 '14 at 07:27
  • @Jack that's a really interesting point - how could I find out? I figure I'd have to manually encode it differently, right? – Jamus Mar 06 '14 at 07:28
  • Here's a link to the uploaded page if that helps explain things: http://eliteshift.com/deve/EliteCards/block-flashcards.php – Jamus Mar 06 '14 at 07:28
  • @Jamus What are you using to upload? Have you got ASCII Upload File-type transfer enabled maybe? – MackieeE Mar 06 '14 at 12:12
  • @MackieeE and Jack I think you may have solved it! Haha, thank you. I'm not sure how to change it in PHPDesigner8, but when I check in the filetype through filezilla it's ASCII. So, thank you very much! :) – Jamus Mar 07 '14 at 00:37
  • @Jamus Ahh! x) Awesome! I might post an answer if that's ok - although on a unrelated note, try out PHPStorm instead if you ever get the chance! =) – MackieeE Mar 07 '14 at 08:35

3 Answers3

1

Possible Solutions:

The HTML Document

  • Adding the appropriate <meta charset="" /> or <meta http-equiv="Content-Type" /> tag for declaring the Content type for the said HTML Document.

  • Setting default charset within php.ini (See SO's Q&A: Setting PHP default encoding to utf-8?)

The FTP/Transfer

  • Making sure the Uploaded .php file was not uploaded in a manner in which could alter your characters to ??? on a un-reconsigned Unicode character.

enter image description here

At Script Writing

  • The above could also happen to some IDE's or Text Editors such as Notepad++ (Below) unless you have the correct Encoding ready:

enter image description here

All this of course is dependant that you're not reading from the Database, this would be an extra layer into the question, although it's a golden rule to go by:

All layers of the Development process must be aligned to the same Charset.

Community
  • 1
  • 1
MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • Thank you, MackieeE! Couldn't have done it without you! Such a simple error, but was beyond me haha. And I've taken your advice, my company might actually buy a commercial license should PHPStorm work well. Thanks very much ! :) – Jamus Mar 08 '14 at 05:33
0

First of all I think It's better to make sure what charset are the contents encoded by when they send to your browser.
If they are encoded by Shift-jis and you force the browser show it in utf-8,it will show some strange characters.
While you know what the charset they are encoded,you can use the mb_convert_encoding to convert them to the charset you want.

bixiaopeng
  • 383
  • 2
  • 11
-1

Place this at the beginning of the code

mysql_set_charset("UTF8", $conn);

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62