utf-8 issue

liv4spd

Member
I added "<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />" to the "page_container" template for all pages. But when I use PHP to render some contents, the Chinese characters can not be displayed properly and become "??????"

Below is part of my php code:

Code:
 @$result .= <<<HTML
<table cellspacing="5" cellpadding="5" border="2" width="100%">
<tr>
    <td colspan="2" style="padding:5px 0px 5px 5px; background-color:#9F9;">该OBD故障码适用于:<b>{$row['make']}</b></td>
</tr>
 
<meta charset="utf-8" /> is already in the default PAGE_CONTAINER template.

Is this imported data? Is it being displayed on a XenForo page or some external page? Can you post the URL?
 
Can you verify the encoding of the actual data in the database? Are these results coming from your XF database or some other database? If you are using some other db connection besides the default then check the charset of that connection.

Obviously the data is not utf8, or it is being corrupted somehow.
 
Hi Jake,

Thank you again!

As I mentioned earlier, I do not have any problem with contents out of the database actually. They are displayed just fine.

The things that gave me throuble are the Chinese characters in my *.php file. I was trying to render them into an Xenforo page.

Please check out my page. "?????????????OBD???????????", which is directly from my *.php file is giving me problems. In contrast, the values inside the table, which is from the database, displays just fine.
http://obdch.com/?search=P0123&submit=查询
 
Last edited:
Thank you very much!

Any idea how I should make my PHP file encoded as UTF-8?

This is my code:

Code:
<?php
class OBD_Search {
    public function getResults() {
    $query = @$_GET['search'];
    $server = "localhost";
    $user = "aaa";
    $password = "aaa";
    $database = "aaa";
    $table = "aaa";
    $obddb = mysql_connect($server, $user, $password) or die("Error Code: 1001");
    mysql_select_db($database) or die("Error Code: 1002");
    mysql_query("SET CHARACTER SET utf8", $obddb);

    $extract = mysql_query("SELECT * FROM $table WHERE code='" . mysql_real_escape_string($query) . "' ORDER BY id ASC");
    $number = mysql_numrows($extract);

    if($number == 0 && @$_GET['search'] != "") {
        @$result = "对不起,我们没有找到您所查询的故障码!";
    } else {

    $i = 0;
    while($row = mysql_fetch_array($extract)) {

  @$result .= <<<HTML
<table cellspacing="5" cellpadding="5" border="2" width="100%">
<tr>
    <td colspan="2" style="background-color:#9F9; padding:5px 0px 5px 10px;">该OBD故障码适用于:<b>{$row['make']}</b></td>
</tr>
<tr>
    <td style="width:63px; padding:5px 0px 5px 0px; text-align:center;">{$row['code']}: </td>
    <td style="padding:5px 5px 5px 5px;">{$row['definition']}</td>
</tr>
<br/>

HTML;
    $i++;
    }
        @$result .= <<<HTML
</table>
<br />
<br />
HTML;
    }
    return @$result;
}
}
//EOF
?>
 
Thanks.

I am using Notepad++ in Windows, so I am able to check the encoding as well. It is utf-8, and Chinese characters display ok in the file.

You've got two pages:
  1. http://www.obdch.com/?search=P0123&submit=查询
  2. http://www.obd-chinese.com/?search=P0123&submit=查询
The first one has the problem described above. Check if the uploaded file is still in utf-8 on your server. It might come from an upload problem.
The second one is working but certain has a "BOM" problem (see the white space at the top of your header). Be sure to upload utf8 file with "no BOM". I think Notepad++ can deal with this.

Proceed also with a simple test to be sure the problem is not coming from elsewhere:
PHP:
<?php
class OBD_Search
{
   public static function getResults($controller,  &$response)
   {
     $response->params['page']['description'] = '成功了';
   }
}

Note that the way of getting info from the database doesn't seem very clean (I didn't the same thing when I start with XenForo). Read this tutorial, it will help you a lot (there are two parts). You should spend a little time on it, it will help you a lot after.
 
Man, you saved my life.

I just changed the encoding from "utf-8" to "utf-8 without BOM". Now everything is working!!!

Thank you so much!


You've got two pages:
  1. http://www.obdch.com/?search=P0123&submit=查询
  2. http://www.obd-chinese.com/?search=P0123&submit=查询
The first one has the problem described above. Check if the uploaded file is still in utf-8 on your server. It might come from an upload problem.
The second one is working but certain has a "BOM" problem (see the white space at the top of your header). Be sure to upload utf8 file with "no BOM". I think Notepad++ can deal with this.

Proceed also with a simple test to be sure the problem is not coming from elsewhere:
PHP:
<?php
class OBD_Search
{
   public static function getResults($controller,  &$response)
   {
     $response->params['page']['description'] = '成功了';
   }
}

Note that the way of getting info from the database doesn't seem very clean (I didn't the same thing when I start with XenForo). Read this tutorial, it will help you a lot (there are two parts). You should spend a little time on it, it will help you a lot after.
 
Back
Top Bottom