Provide zblog template_zblog them_wordpress template download and customization

PHP saves the txt file with the BOM header added and removes the BOM header from the txt file when reading

Tianxing Studio 2023-05-02 19:09 course three hundred and seventy-two 0 Comments

Recently, a user reported that the uploaded txt file was opened directly with a browser. Some browsers display it normally Garbled code then Tianxing Studio After looking at it, it is determined that there is Chinese in the txt file and the encoding format is incorrect. The simplest solution is to save the txt file on the computer and select "UTF-8 BOM", as shown in the following figure:

 PHP saves the txt file with added BOM header and removes the txt file with BOM header when reading. txt text. BOM header tutorial page 1

This article is to share another method of using php to handle txt coding.

First, use the php save tape Bom head Of Txt file , the code is as follows:

 $str='Here are some Chinese'; //String $str = chr(0xEF).chr(0xBB).chr(0xBF).$ str; //Plus bom head $txt_path = '1.txt'; //File Save Path $fp = fopen($txt_path, "w"); fwrite($fp,$str); fclose($fp);

Then, when PHP reads the txt text file, it needs to remove the BOM header.

First write a function to process the BOM header:

 //This function comes from: function del_bom($contents){     $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) {         $rest = substr($contents, 3);         return $rest;     }else{         return $contents;     } }

Then read txt, remove the BOM header, and then read:

 $txt_path = '1.txt'; //Txt file path $upload_txt = file_get_contents($txt_path); $upload_txt = del_bom($upload_txt); echo $upload_txt;

The above is a detailed tutorial of using PHP to process the BOM header of txt text files.

Can't find a tutorial that can solve your problem?

You can try to search or ask questions directly online. We also provide charging technical support. If you need it, you can contact us online.

Online questions Online Service

Related recommendations

welcome you Comment: Cancel Reply

 Please fill in the verification code
  • Latest articles
  • Hot article ranking
  • Most Comments
Label aggregation
  • Sign in
  • Registered account Forgot your password?
  • register
  • Already have an account? Direct login Forgot your password?
  • Social account login