Blank line problem caused by Php reference file (UTF-8)_PHP tutorial

WBOY
Release: 2016-07-13 10:57:43
Original
1029 people have browsed it

Many people may encounter the problem of blank lines when importing files using UTF8 encoding in PHP. Now I will introduce to you the solution to the blank lines of quoted files.

Referenced files refer to blank lines caused by include and require_once referencing other files

This is actually after you edit the UTF-8 file with Notepad, although you still save it in UTF-8 format, the system will automatically add two BOM tags to the file, but IE can only ignore one, and the other is blank. OK.
The solution is:

Do not use Notepad to edit UTF-8 files. Use other tools, such as Dreamw, EmEditor, Visual Studio 2008 and other editing tools, to edit UTF-8 files and save them.

After editing two of my sites with Notepad, there were blank lines at the top. At first I thought it was a css setting problem, but it turned out to be caused by the BOM tag.

There is another way, if you have a large number of files like this, we can use a php function to do it

Copy the following code into a PHP file, then put it in the directory where the BOM header needs to be cleared and run it. You can clear the blank first lines of PHP in batches. It is so powerful.

The code is as follows
 代码如下 复制代码

 
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")."
";  
    }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.");  
  } 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);  
}  
?>

Copy code
if (isset($_GET['dir'])){ //Set the file directory
$basedir=$_GET['dir']; $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."); } 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); } ?> http://www.bkjia.com/PHPjc/632081.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632081.htmlTechArticleMany people may encounter that if it is utf8 encoded in php, a blank line will appear when we import the file. Question, let me introduce to you the solution to the problem of citing blank lines in files. Quote...
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