Home > Article > Backend Development > What does php bom mean?
php bom is a mark used to determine which Unicode encoding a text file is; 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.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
What does php bom mean?
PHP-BOM
1. Problem description: All ajax requests returned using the control layer will have a small red dot attached in front. After checking, it is because there is a BOM in front of the file. Although it was later proven that not all files contained BOM, it caused problems with the data returned by all files.
2. So, what is BOM?
Answer: In a utf-8 encoded file, the BOM is in the file header, occupying three bytes, and is used to indicate that the file belongs to utf-8 encoding. There are many softwares that recognize the BOM header, but there are still Some cannot recognize the BOM header. For example, PHP cannot recognize the BOM header. This is also the reason why an error occurs after editing the UTF-8 encoding with Notepad.
3. The solution is to find some batch processing BOM scripts online and run them. You can also run tools like editplus and Ultraedit to solve the problem.
4. Finally, provide a BOM removal file and just run it.
echo '当前查找的目录为:'.$basedir.'当前的设置是:'; echo $auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />'; checkdir($basedir); function checkdir($basedir){ if($dh=opendir($basedir)){ while (($file=readdir($dh)) !== false){ if($file != '.' && $file != '..'){ if(!is_dir($basedir.'/'.$file)){ echo '文件: '.$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并已自动去除</font>'); }else{ return (' <font color=red>找到BOM</font>'); } }else{ return (' 没有找到BOM'); } } function rewrite($filename,$data){ $filenum=fopen($filename,'w'); flock($filenum,LOCK_EX); fwrite($filenum,$data); fclose($filenum); } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php bom mean?. For more information, please follow other related articles on the PHP Chinese website!