Last active
January 6, 2020 08:53
-
-
Save welefen/7746175 to your computer and use it in GitHub Desktop.
php判断文件编码和检测文件是否有UTF-8头信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 获取内容的编码 | |
* @param string $str | |
*/ | |
function get_encoding($str = "") { | |
$encodings = array ( | |
'ASCII', | |
'UTF-8', | |
'GBK' | |
); | |
foreach ( $encodings as $encoding ) { | |
if ($str === mb_convert_encoding ( mb_convert_encoding ( $str, "UTF-32", $encoding ), $encoding, "UTF-32" )) { | |
return $encoding; | |
} | |
} | |
return false; | |
} | |
/** | |
* | |
* 检测utf-8内容是否含有BOM头信息 | |
* @param string $str | |
*/ | |
function utf8_has_bom($str) { | |
$chars = substr ( $str, 0, 3 ); | |
$bom = chr ( 0xEF ) . chr ( 0xBB ) . chr ( 0xBF ); | |
return $chars === $bom; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment