PHP Read txt file Chinese garbled solution

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags click code copy create display file notepad php

1: If the page is open garbled, don't worry, do not do any editing. Remember。
2: Then click the menu to Modify-"page Properties-" title/code
3: In the Code select Simplified Chinese (gb2321)
4: Click Reload-"OK"

Create a new Notepad (not a clipboard!) , and then copy the file that you want to read into this Notepad file and save it. Use the following code to read the Notepad file 1303275.txt: found that when the page is encoded as gb2312 all normal display. Changed to UTF8 code, the number is normal, Chinese characters for garbled, which also belong to the normal

<?php Tutorial
$file = fopen ("1303275.txt", "R");//read-only Open text file
while (! feof ($file))//When file does not end
{
$line =fgets ($file);//read one line to $line variable
Echo $line. <br/> ";
}
Fclose ($file);//Close text file

?>
The above method is more stupid but also solves the problem, below I provide a no matter what txt text will not disorderly solution.

/*
@params $str input character $type need to get the encoding
@author Long Line
*/
function Autoiconv ($str, $type = "Gb2312//ignore") {

$utf 32_big_endian_bom = Chr (0x00). Chr (0x00). Chr (0XFE). Chr (0xff);
$utf 32_little_endian_bom = Chr (0xff). Chr (0XFE). Chr (0x00). Chr (0x00);
$utf 16_big_endian_bom = Chr (0xFE). Chr (0xff);
$utf 16_little_endian_bom = Chr (0xff). Chr (0XFE);
$utf 8_bom = Chr (0xef). Chr (0XBB). Chr (0XBF);

$first 2 = substr ($str, 0, 2);
$first 3 = substr ($str, 0, 3);
$first 4 = substr ($str, 0, 3);

if ($first 3 = = $utf 8_bom) $icon = ' utf-8 ';
ElseIf ($first 4 = = $utf 32_big_endian_bom) $icon = ' utf-32be ';
ElseIf ($first 4 = = $utf 32_little_endian_bom) $icon = ' Utf-32le ';
ElseIf ($first 2 = = $utf 16_big_endian_bom) $icon = ' utf-16be ';
ElseIf ($first 2 = = $utf 16_little_endian_bom) $icon = ' Utf-16le ';
else {$icon = ' ASCII '; return $str;}

Return Iconv ($icon, $type, $STR);

}

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.