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);
*/
?>
I don't quite understand the problem you have, if you don't want the special characters to be converted into format
\u....
The receiver has to reverse the encoding, normally it does it automatically, for example in
php
usingjson_decode(data_json)
it just converts it.But you still want them not to be converted, use the option
JSON_UNESCAPED_UNICODE
Test
Official PHP documentation json_decode
Hello dear try adding this line in your connection file:
You only have to add that and it will be displayed correctly, you no longer need to put in the head of each file, only in the connection file.
I have also encountered the same problem and the solution has been combining two of the answers that they gave you. Doing this:
I hope it helps you
You can fix it with this function:
Hello, it is very simple with just this.
utf8_encode() which is intended to encode strings to UTF-8 format. It is very useful when you work with api rest or xml
Cheers
It is my first contribution :) here I leave my solution.
I HOPE IT SERVES YOU
in my case that I am using composer with slim, it has been solved by putting
before making a request.
An example of a get request would be this:
It worked for me just putting:
If I read the returned JSON text without converting it to a JavaScript object, it does display the characters
JES\u00daS
otherwise it doesn't.