解决php中字符串输出一样但比较不相等(编码不同导致长度不一样)

问题:在phpstorm中用AES产生的一段加密代码,发现解密的过程中字符串的长度和编码方式都不一样了。代码如下:

header("Content-type:text/html;charset=utf-8");
$secret1 = "rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI=";
$secret2 = 'rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI=';
$secret3 = 'rZa9GneIzd9MNyoTv/Ghpk2V6ZHD0KeQC7t0ymEH7cI=';

var_dump(trim($secret1));
echo "
"; var_dump(trim($secret2)); echo "
"; var_dump($secret3); echo "
"; echo mb_detect_encoding($secret1, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); echo "
"; echo mb_detect_encoding($secret2, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); echo "
"; echo mb_detect_encoding($secret3, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));

解决php中字符串输出一样但比较不相等(编码不同导致长度不一样)_第1张图片
通过CSDN问答,知道BOM问题,解决方案如下:

function removeBom($string)//去除BOM
{
	if (substr($string, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
		return substr($string, 3);
	}
	return $string;
}

function ConvertUTF8ToGB2312($str)//转化为统一编码;
{
	return iconv("UTF-8", "GB2312//IGNORE", $str);
}

你可能感兴趣的:(php)