Home > Backend Development > PHP Tutorial > PHP code sharing for removing BOM header

PHP code sharing for removing BOM header

WBOY
Release: 2016-07-25 08:51:29
Original
868 people have browsed it
This article introduces the example code to remove the BOM header in PHP. The BOM is in the header of the file and takes up three bytes. Sometimes it is necessary to remove this information. The examples given in this article can be used as a reference.

The php code to remove the BOM header is as follows:

<?php
/**
* 功能:去除bom头信息
* edit:bbs.it-home.org
*/
if (isset($_GET['dir'])){ //设置文件目录
$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._csdn.net</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);
}
?>
Copy after login

Attached, BOM header information description. 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.

PHP cannot recognize the BOM header, and an error will occur after editing the UTF-8 encoding with Notepad.

Remove the bom, you can do this: 1. How to remove BOM header in editplus After the editor is adjusted to the UTF8 encoding format, there will be a string of hidden characters (that is, BOM) in front of the saved file, which is used by the editor to identify whether the file is UTF8 encoded. Run Editplus, click Tools, select Preferences, select the file, and select UTF-8 identification. Always delete the signature, and then edit and save the PHP file. The PHP file will not have a BOM.

2. How to remove BOM in ultraedit After opening the file, select the encoding format in the save as option (utf-8 without BOM header), and confirm.

3. A specially written program to remove the BOM header of a file is now released and can be placed in the project root directory and then run.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template