Website file Batch transcoding _php tutorial

Source: Internet
Author: User
Citation: Due to database requirements, the database is changed from the original gbk2312 code to Utf-8, in order to facilitate data interaction to reduce the problem caused by coding inconsistencies,

Need to transcode the site to the entire station (GB2312-->UTF-8)

Find a batch transcoding tool on the Internet

Note: 1. This software supports the selection of files, or the selection of directories, there are some optional types, can also be all files, provide convenient and also be careful. To see if there are files in the selected file that do not need to be transcoded, such as different encoded files, pictures, etc., you must not go together.

2. No deduplication function, so be careful not to repeat the selection of files (repeat selection will be what, I also try)
3. If you check "Keep file backup", then each file has a corresponding Bak file generation, because my project has been managed with git, so do not need to backup (git own recovery function), how to back up the law to see the specific situation, anyway, this big moves still have to be cautious.


Second, remove BOM header
Use the EditPlus Editor to open the file you just transcoded, the bottom status bar display is encoded as "utf-8+", which contains the BOM header.

What is a BOM? Quote netizens a word "in the Utf-8 encoded file BOM in the file head, occupies three bytes, used to indicate that the file belongs to Utf-8 encoding, now has a lot of software to identify the BOM head, but also some do not recognize the BOM header, such as PHP can not identify the BOM header, This is also the reason why the error occurs when you edit Utf-8 encoding with Notepad.

In this way, when PHP executes the program will be the BOM header as the content output, when encountering the requirements can not be output, such as session_start () will be wrong.

Single file, use the EditPlus editor to open a file saved as no ' utf-8 ' (i.e. no BOM).

For so many files, there are users to share a script, rapid batch of accurate removal of the BOM head (did not find the original author, in this thank high man to share ~), in the root directory of the transcoding file to build a PHP file, the following code is copied into the URL, enter the access address, execution can:

if (Isset ($_get[' dir ')) {//Set file directory
$basedir =$_get[' dir '];
}else{
$basedir = '. ';
}
$auto = 1;
Checkdir ($basedir);
function Checkdir ($basedir) {
if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file! = '. ' && $file! = ' ... ') {
if (!is_dir ($basedir. " /". $file)) {
echo "FileName: $basedir/$file". Checkbom ("$basedir/$file"). "
";
}else{
$dirname = $basedir. " /". $file;
Checkdir ($dirname);
}
}
}
Closedir ($DH);
}
}
function Checkbom ($filename) {
Global $auto;
$contents = file_get_contents ($filename);
$charset [1] = substr ($contents, 0, 1);
$charset [2] = substr ($contents, 1, 1);
$charset [3] = substr ($contents, 2, 1);
if (Ord ($charset [1]) = = 239 && ord ($charset [2]) = = 187 && ord ($charset [3]) = = 191) {
if ($auto = = 1) {
$rest = substr ($contents, 3);
Rewrite ($filename, $rest);
Return ("BOM found, automatically removed._http://www.k686.com");
} else {
Return ("BOM found.");
}
}
else return ("BOM not Found.");
}
function rewrite ($filename, $data) {
$filenum = fopen ($filename, "w");
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>
Third, use the powerful Zendsdio batch lookup to replace the gb2312 encoded in the HTM declared as Utf-8

Note: The new Zend Project, see if the HTM is normal display, if it is garbled, see the Project HTM encoding is set to Utf-8, select the project, Global Search (ctrl+h) "charset=gb2312" bulk replaced by "Charset=utf-8",

Note: It is possible that some files that are introduced outside the project need to be declared as gb2312, so it is necessary to exclude these exceptions, not to be replaced together, and to replace the files that have been transcoded.

In addition, there may be spaces such as "charset= gb2312", no spaces, all kinds of writing are searched. In case there is a slip.

Four, and then focus on the PHP file in the gb2312 (or GBK), to combine the context of the contextual logic to determine whether to replace. All kinds of writing also need to search, such as utf8,utf-8,gbk,gb2312, etc.


http://www.bkjia.com/PHPjc/477921.html www.bkjia.com true http://www.bkjia.com/PHPjc/477921.html techarticle citation: Due to database requirements, the database is changed from the original gbk2312 code to Utf-8, in order to facilitate data interaction to reduce the problem caused by inconsistent coding, the site will need to transcode (gb2312--utf-8) ...

  • 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.