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. Here's the test data I was running through a php tester:
$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"}
{"discreci\\x{f3}n","prudencia","experiencia","vida"}
discreci\\xf3n,prudencia,experiencia,vida
discreci\\xf3n
prudencia
experiencia
vida
discreci\\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. if you want ó
use stripcslashes to activate the embedded \\
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.