TP Small Problem

Source: Internet
Author: User
Tags php json
Use TP traverse the public folder under a directory of all the file names, test, found that the Chinese file names are garbled,
The files are UTF-8 encoded. Native PHP tests are used without this problem. Special to ask.
Here is the code section in TP:
Public Function Index () {        $dir = "./public/articles";//Article directory        $data = $this->dirlist ($dir);        $arr =array ();        foreach ($data as $v) {            $arr []= $v;        }       Print_r ($arr); Chinese garbled       $this->ajaxreturn ($arr);    }    Public Function Dirlist ($dir) {            $arr =scandir ($dir);            foreach ($arr as $val) {                if ($val! = '. ' && $val! = ') {                    $arr 2[]= $val;                }            }        return $arr 2;    }




Reply to discussion (solution)

The Chinese file name of the window system is GBK encoded

Do you think it's a minor problem? Not too!
If even the living environment is unclear, it's not a big problem.

Do you think it's a minor problem? Not too!
If even the living environment is unclear, it's not a big problem.


Even if I change the file to GBK encoding, it is the same result. Native PHP is normal and does not garbled. So I don't know what's going on.
In addition to spit slot, PHP open a Chinese text, but also to a code, it is too earth.

How to set header before Ajaxreturn?
Header (' Content-type:application/json;charset=utf-8 ');

How to set header before Ajaxreturn?
Header (' Content-type:application/json;charset=utf-8 ');


No. I added it later.
Header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');

It is possible to print out Chinese, but the data returned by Ajax is shown to be null, which is really depressing.


How to set header before Ajaxreturn?
Header (' Content-type:application/json;charset=utf-8 ');


No. I added it later.
Header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');

It is possible to print out Chinese, but the data returned by Ajax is shown to be null, which is really depressing.


Header (' CONTENT-TYPE:TEX/HTML;CHARSET=GBK ');
The Ajaxreturn function is output by default in JSON format, you declare the header as text/html, return must be null Ah, instead of Application/json trial trial No

1, the default character set of Linux system is Utf-8
2. The default character set for Windows system in Simplified Chinese is GBK
3, the working character set of TP is Utf-8

So TP doesn't have a problem with Linux.
But there's a garbled problem in Windows.

So after you read the file name, you do a character set conversion based on the operating system

PHP JSON functions only support the Utf-8 character set

PHP Open a Chinese text, but also to a code, it is too dirt
It's just that you put yourself in trouble for yourself.

1, the default character set of Linux system is Utf-8
2. The default character set for Windows system in Simplified Chinese is GBK
3, the working character set of TP is Utf-8

So TP doesn't have a problem with Linux.
But there's a garbled problem in Windows.

So after you read the file name, you do a character set conversion based on the operating system

PHP JSON functions only support the Utf-8 character set

PHP Open a Chinese text, but also to a code, it is too dirt
It's just that you put yourself in trouble for yourself.


My text whether is GBK code or UTF-8, return is all garbled. The text name that is accepted over Ajax shows null.
and PHP Open the text of Chinese name, if not to turn a code open, is garbled, what good way?

Display different encoded data in the same environment, always with garbled appearance

The file name under Windows needs to execute Iconv (' GBK ', ' UTF-8 ', $name); To UTF8 encoded output.

Windows read Chinese file name is to go to encode
The best way to do this is to change Linux, for example, to install a Linux virtual machine on Windows or something
Otherwise, a read file function, read the file unified with that function, function write conversion

The file name under Windows needs to execute Iconv (' GBK ', ' UTF-8 ', $name); To UTF8 encoded output.


Does the path include Chinese as well? Try it like or garbled.
$data = $this->dirlist ($dir);
Print out data must be turned into GBK to display properly, UTF8 not. Then the return value on the Ajax side is null.

Windows read Chinese file name is to go to encode
The best way to do this is to change Linux, for example, to install a Linux virtual machine on Windows or something
Otherwise, a read file function, read the file unified with that function, function write conversion



Turn the code is not successful, can you point it down?
$data = $this->dirlist ($dir); Over here
Print out data must be prefixed with header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');
In order to display properly in the browser, UTF8 not. Then the return value on the Ajax side is null.

Gbk->utf8
$file = mb_convert_encoding ($file, ' utf-8 ', ' utf-8,gbk,ascii ');
Utf8->gbk
$file = Iconv (' UTF-8 ', ' Gbk//ignore ', $file);

Gbk->utf8
$file = mb_convert_encoding ($file, ' utf-8 ', ' utf-8,gbk,ascii ');
Utf8->gbk
$file = Iconv (' UTF-8 ', ' Gbk//ignore ', $file);


Thank you. Read a blog last night to get it done, in the traversal of each element to convert the code, and then into the array can be.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.