[英]utf8_encode not encoding/converting character
I'm trying to use utf8_encode to display some accented characters (for purposes of putting them into a json), but for some reason it seems to fail and I don't understand why.我正在尝试使用 utf8_encode 来显示一些重音字符(为了将它们放入 json 中),但由于某种原因它似乎失败了,我不明白为什么。 Here's the test data I was running through a php tester:这是我通过 php 测试器运行的测试数据:
$data = '{"14446":"discreci\u00f3n","14450":"prudencia","14461":"experiencia","14491":"vida"}';
$data = preg_replace('|u00([A-Fa-f0-9][A-Fa-f0-9])|', 'x{$1}', $data);
echo $data;
echo "\r\n\r\n";
$data = preg_replace('/"\d*":/','',$data);
echo $data;
echo "\r\n\r\n";
$data = preg_replace('/[{}"?!]/','',$data);
echo $data;
echo "\r\n\r\n";
$data_array = explode(",", $data);
foreach ($data_array as $data) {
echo $data;
echo "\r\n\r\n";
}
echo utf8_encode($data_array[0]);
echo "\r\n\r\n";
echo utf8_encode("discreci\xf3n");
Which output:哪个输出:
{"14446":"discreci\\x{f3}n","14450":"prudencia","14461":"experiencia","14491":"vida"} {"14446":"discreci\\x{f3}n","14450":"prudencia","14461":"experiencia","14491":"vida"}
{"discreci\\x{f3}n","prudencia","experiencia","vida"} {"discreci\\x{f3}n","prudencia","experiencia","vida"}
discreci\\xf3n,prudencia,experiencia,vida Disreci\\xf3n,prudencia,experiencia,vida
discreci\\xf3n离散\\xf3n
prudencia审慎
experiencia体验
vida维达
discreci\\xf3n离散\\xf3n
discreción自由裁量权
As you can see, it converts it if I enter it directly, but it doesn't convert it if it receives it as a variable, even if it's been surrounded with quotation marks eg.:如您所见,如果我直接输入它,它会转换它,但如果它作为变量接收它,它不会转换它,即使它被引号包围,例如:
foreach ($data_array as $data) {
$data = '"' . $data . '"';
echo $data;
echo "\r\n\r\n";
}
doesn't convert it either.也不转换它。
So - what am I doing wrong?所以 - 我做错了什么?
you create a string that contains \\xf3
these are all ASCII characters.您创建一个包含\\xf3
的字符串,这些都是 ASCII 字符。 if you want ó
use stripcslashes to activate the embedded \\
如果你想ó
使用 stripcslashes 来激活嵌入的\\
echo utf8_encode(stripcslashes($data_array[0]));
But on the other hand但另一方面
foreach( json_decode($data,1) as $v ) echo $v ,"\n\n" ;
Works just fine on the original $data
(before preg_replace), and supports all Unicode not just a subset.在原始$data
(preg_replace 之前)上工作得很好,并且支持所有 Unicode 而不仅仅是一个子集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.