Home  >  Article  >  Backend Development  >  What does php bom mean?

What does php bom mean?

藏色散人
藏色散人Original
2023-02-20 09:43:403151browse

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.

What does php bom mean?

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?&#39;检测文件BOM同时去除检测到BOM文件的BOM<br />&#39;:&#39;只检测文件BOM不执行去除BOM操作<br />&#39;;
checkdir($basedir);
function checkdir($basedir){
    if($dh=opendir($basedir)){
        while (($file=readdir($dh)) !== false){
            if($file != &#39;.&#39; && $file != &#39;..&#39;){
                if(!is_dir($basedir.&#39;/&#39;.$file)){
                    echo &#39;文件: &#39;.$basedir.&#39;/&#39;.$file .checkBOM($basedir.&#39;/&#39;.$file).&#39; <br>&#39;;
                }else{
                    $dirname=$basedir.&#39;/&#39;.$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 (&#39; <font color=red>找到BOM并已自动去除</font>&#39;);
        }else{
            return (&#39; <font color=red>找到BOM</font>&#39;);
        }
    }else{
        return (&#39; 没有找到BOM&#39;);
    }
}
function rewrite($filename,$data){
    $filenum=fopen($filename,&#39;w&#39;);
    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!

Statement:
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