search

Home  >  Q&A  >  body text

Why are the same strings encoded differently in PHP?

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

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

The same string displays different codes when detecting the code. What is the problem?

20190506123102.png

保哥后院保哥后院2107 days ago1034

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2019-05-06 16:33:50

    There will be different encoding schemes for different character sets. The current one is UTF8

    reply
    0
  • 保哥后院

    Thank you Teacher Peter-Zhu. This is an encryption code generated using AES in phpstorm. It is found that the length and encoding method of the string are different during the decryption process. Now in the process of solving programming, how to avoid such problems. Currently, you can use the BOM special invisible characters in the file to clear the function removeBom($string) { if (substr($string, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { return substr($string, 3); } return $string; }

    保哥后院 · 2019-05-07 10:28:21
  • Cancelreply