My json returns invalid character in PHP

Asked

Viewed 433 times

0

I need to assemble a JSON, in the PHP, to return data to Angularjs, but one of the Object data from JSON is coming with the letter "á" with another character. How can I correct this? NOTE: The name is accented in the BD as it was inserted into the BD with accent. BD is set to utf8-bin.

My php:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

header('Access-Control-Allow-Origin: *');
header('Content-Type: text/html; charset=utf-8');

include_once("conPDO.php");
$pdo = conectar();

$idUsuario = $_GET['idUsuario'];

$pegaUsuario=$pdo->prepare("SELECT * FROM usuarios WHERE idUsuario=:idUsuario");
$pegaUsuario->bindValue(":idUsuario", $idUsuario);
$pegaUsuario->execute();

$return = array();

while ($linha=$pegaUsuario->fetch(PDO::FETCH_ASSOC)) {
    array_push($return, $linha);
}
print_r($return[0]);
//echo json_encode($return[0]);

?>

And see how the json Object appears on the console, see the name.

inserir a descrição da imagem aqui

  • Possible duplicate of Doubt with charset=iso-8859-1 and utf8. Try everything from the answer http://answall.com/a/43205/3635, including the exec('SET CHARACTER SET utf8');

  • Already solved the problem?

  • @Guilhermenascimento, I’m using utf8-bin in the database, so the database accepts accented letters. I just don’t understand why this json, "distorts" the letter "á".

  • Why did the $pdo->exec('SET CHARACTER SET utf8'); like I said before.

3 answers

1


Another solution, if the aforementioned does not work, is you go through the object that transform your attributes into UTF8:

for(x in objeto){
  objeto[x] = utf8_encode(objeto[x]);
}

Edited

Solution:

while ($linha=$pegaUsuario->fetch(PDO::FETCH_ASSOC)) {
    $linha['nome'] = utf8_encode($linha['nome']); 
    array_push($return, $linha);
}
  • I’ve tried that, but give "a," turn "?"

  • try using this other function: mb_convert_encoding(object[x], "UTF-8", mb_detect_encoding(object[x], "UTF-8, ISO-8859-1, ISO-8859-15", true));

  • If it does not work, before the json print, from an echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">', this will solve

  • returned this in the console: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">Array ( [idUsuario] => 3 [idCep] => 3 [typeUsuario] => C [name] => Fl via Schneider [user] => )

  • I believe that the data does not appear on the screen, in html, because the name is with accent, because if I test with name without accent, then the data appear on the screen, you understand?

  • when printing json, you tested echo json_encode($object);die;?

  • I just tried Julyano, but nothing happens... No data appears on the screen.

Show 3 more comments

0

You can use various php functions to convert your character into an html character, ansii etc use this function before saving to the database

htmlentities($suaString) 

this will convert your character into a character &aacute; There are several factors that may be generating this charcode error check if your file is in utf-8 recommend this formatting, if there is one include from another check that it is also formatted use the meta tag <meta charset="utf-8"> html also use the header in php

header('Content-Type: text/html; charset=utf-8')

Remomendo work with notpad++ you can lower it here

  • htmlentities? To my knowledge only htmlentities are used when we use HTML and PHP, but in this case, I am working with Angularjs.

  • you put in your php and json tags

0

As an alternative, and decreasing some lines of code, use the fetchAll to get a array containing the elements of the returned lines in the sql and the function array_map to apply the utf8_encode in all elements of array:

$return = $pegaUsuario->fetchAll(PDO::FETCH_ASSOC);
$encoded = array_map("utf8_encode", $return);

var_dump(json_encode($encoded, JSON_UNESCAPED_UNICODE));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.