Question:
I have problems with accents and special characters, generating a json with php in the accents, for example:
T\u00c3\u00a9cnologia = Técnologia
I already tried to fix it with mysqli_set_charset($conexion, "utf8");
but nothing.
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,x-prototype- version,x-requested-with');
//header("Content-type: application/json; charset=utf-8");
$server = "localhost";
$user = "root";
$pass = "";
$bd = "u703093142_wifi";
//Creamos la conexión
$conexion = mysqli_connect($server, $user, $pass,$bd)
or die("Ha sucedido un error inexperado en la conexion de la base de datos");
//generamos la consulta
$sql = "select upload.*,promociones.nombre_input
from upload
inner join promociones on upload.idGaleria=promociones.imagen_gallery limit 4";
mysqli_set_charset($conexion, "utf8"); //formato de datos utf8
if(!$result = mysqli_query($conexion, $sql)) die();
//$clientes = array(); //creamos un array
/*
while($row = mysqli_fetch_array($result))
{
$titulo=$row['titulo'];
utf8_decode($nombre_input=$row['nombre_input']);
$data[] = array('nombre_input'=> $nombre_input,'titulo'=> $titulo);
}
*/
while($row = mysqli_fetch_assoc($result)){
$data[]=array_map('utf8_encode', $row);
}
$close = mysqli_close($conexion)
or die("Ha sucedido un error inexperado en la desconexion de la base de datos");
//Creamos el JSON
$json_string = json_encode($data);
//echo $json_string;
echo '{"datos":'.$json_string."}";
//Si queremos crear un archivo json, sería de esta forma:
/*
$file = 'clientes.json';
file_put_contents($file, $json_string);
*/
?>
Answer:
I don't really understand the problem you have, if you don't want the special characters to be converted into \u....
format \u....
The receiver has to reverse the encoding, normally it does it automatically, for example in php
using json_decode(data_json)
it converts it without further json_decode(data_json)
.
But you still want them not to convert, use the JSON_UNESCAPED_UNICODE
option
Test
$response = "Técnologia";
echo json_encode($response); //T\u00e9cnologia
echo json_encode($response, JSON_UNESCAPED_UNICODE); //Técnologia
Official PHP documentation json_decode