1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

utf-8 issue

Discussion in 'XenForo Questions and Support' started by liv4spd, Oct 18, 2013.

  1. liv4spd

    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>
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    <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?
     
  3. liv4spd

    liv4spd Member

    Thank you for the reply, Jake.

    I am trying to display some MYSQL search results on XenForo page. Funny thing is that the contents from the MYSQL database displays perfectly, just the contents from the PHP file are giving me trouble.

    http://obdch.com/?search=P0123&submit=查询
     
  4. liv4spd

    liv4spd Member

    Is that meta tag applicable to newly rendered contents?
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
  6. liv4spd

    liv4spd Member

    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: Oct 18, 2013
  7. Mike

    Mike XenForo Developer Staff Member

    This simply indicates that your PHP file isn't actually encoded as UTF-8.
     
  8. liv4spd

    liv4spd Member

    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
    ?>
    
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is a function of your text editor. I use gedit in OS X which lets you specify the encoding when saving a file.
     
  10. liv4spd

    liv4spd Member

    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.
     
  11. cclaerhout

    cclaerhout Well-Known Member

    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.
     
  12. liv4spd

    liv4spd Member

    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!


     

Share This Page