Home > Article > PHP Framework > How to remove bom in thinkphp
Thinkphp method to remove BOM: 1. Run Editplus, click "Tools", select "Preferences", and then set the "UTF-8 Identity"; 2. Open the file through ultraedit and select the encoding in the Save As option Select "utf-8 without BOM header" in the format; 3. Use the "92wcms.php" program in the directory to remove the BOM.
The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.
How to remove bom in thinkphp?
thinkphp clear BOM method
In a utf-8 encoded file, the BOM is in the header of the file and occupies three bytes to indicate that the file belongs to UTF-8 encoding, now there are many software that recognize BOM header, but there are still some that cannot recognize BOM header. For example, PHP cannot recognize BOM header. This is also the reason why an error occurs after editing UTF-8 encoding with Notepad. The BOM is only available when "Notepad" is used to store it as UTF-8 under Windows. You can use WINHEX to delete the first 2 bytes. In the encoding settings in Dreamweaver, you can set whether to include a BOM. Generally, as long as the output of PHP is not a picture (GDI Stream), the BOM will not cause problems.
The simple ways to remove the BOM header are the following two:
1. How to remove the BOM header with editplus
After the editor is adjusted to UTF8 encoding format, save it There will be a string of hidden characters (that is, BOM) in front of the file, which is used by the editor to identify whether the file is UTF8 encoded. Run Editplus, click Tools, select Preferences, select the file, select UTF-8 identification, always delete the signature, and then edit and save the PHP file without BOM.
2. Ultraedit method to remove BOM header
After opening the file, select the encoding format in the Save As option (utf-8 without BOM header), and confirm it.
For PHP programs that need to remove the Bom header, you can also use the 92wcms.php program in the directory to remove it.
Please use the following code to run it once
<?php if(isset($_GET['dir'])){ //config the basedir $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")."<br>"; }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("<font color=red>BOM found,automatically removed.</font>"); } else{ return("<font color=red>BOM found.</font>"); } } else return("BOM Not Found."); } function rewrite ($filename, $data) { $filenum= fopen($filename, "w"); flock($filenum, LOCK_EX); fwrite($filenum, $data); fclose($filenum); } ?>
Recommended study: "thinkPHP Video Tutorial"
The above is the detailed content of How to remove bom in thinkphp. For more information, please follow other related articles on the PHP Chinese website!