BOM header

    技术2022-05-20  119

    When you use some editer like notepad etc to edit file and save it to encoding utf-8 , the editer will add a flag in the front of the file , use hexadecimal express was EF BB BF . So convenient in some condition , but in some PHP file , it will occur some error just like: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent...

    Very confuzed and waste of time.

    So I write a function to cut the BOM to make php file can work correctly:

    /** * if the $file had BOM head, cut it * @param string $file * @return void */ function cutBOM($file){ if (FALSE !== ($contents = file_get_contents($file))){ $char = array(); $char[] = substr($contents,0,1); $char[] = substr($contents,1,2); $char[] = substr($contents,2,3); //EF 239 BB 187 BF 191 if (ord($char[0]) == 239 && ord($char[1]) == 187 && ord($char[2]) == 191){ $newContents = substr($contents, 3); file_put_contents($file, $newContents); echo 'cutBOM success'; }else echo 'no BOM need to cut'; }else { echo "can't get file $file content"; } } //example: $file = dirname(__FILE__).'/index2.php'; cutBOM($file); 


    最新回复(0)