for example
$data = 'Baidu�Tencent,Alibaba';
$data1 // It is a variable. When printed, it is 'Baidu�Tencent, Alibaba'
var_dump($data);
var_dump($data1);
Print results:
$data: string(24) "Baidu�Tencent, Alibaba"
$data1:string(22) "Baidu�Tencent, Alibaba"
$keywordsData = json_encode($data, JSON_UNESCAPED_UNICODE);
$keywordsData1 = json_encode($data1, JSON_UNESCAPED_UNICODE);
var_dump($keywordsData);
var_dump($keywordsData1);
Print results:
keywordsData:string(26) ""Baidu�Tencent,Alibaba""
keywordsData1:bool(false)
Why is this? I want to get the variable directly, but now I encounter this problem, please answer, thank you
The two variables are different. Although they are both strings, one has a length of 24 and the other has a length of 22. The second one is obviously not UTF8 encoded.
json_encode
will of course be wrong.You can first detect the string encoding,
mb_detect_encoding
, and then convert it to UTF8,mb_convert_encoding
.