Link to home
Start Free TrialLog in
Avatar of IvanGarcete
IvanGarceteFlag for Paraguay

asked on

JSON string with incorrect spanish characters

Hello, I'm working with PHP producing a JSON string I retrieve with a JQuery AJAX call, but the spanish characters doesn't appear at all. In fact each string is cut off when they appear. I need to know how to fix this so that they appear correctly.

This is my PHP code:
<?php require_once('Connections/Arcadiapolis.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_rsItem = 5;
$pageNum_rsItem = 0;

if (isset($_GET['pageNum_rsItem'])) {
	$pageNum_rsItem = $_GET['pageNum_rsItem'];
}

$startRow_rsItem = $pageNum_rsItem * $maxRows_rsItem;
mysql_select_db($database_Arcadiapolis, $Arcadiapolis);

$query_rsItem = "SELECT 
					item.ItmCod,
					item.ItmName,
					item.ItmDescription
				FROM
					item
";

$query_limit_rsItem = sprintf("%s LIMIT %d, %d", $query_rsItem, $startRow_rsItem, $maxRows_rsItem);

$rsItem = mysql_query($query_limit_rsItem, $Arcadiapolis) or die(mysql_error());

$rows = array();

while($r = mysql_fetch_assoc($rsItem)) {
    $rows[] = $r;
}

print json_encode($rows);
mysql_free_result($rsItem);
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IvanGarcete

ASKER

Hi leakim971, each table in the database is using the character set utf8, collation utf_general_ci, and the database itself is set the same way.

I've already tried to set the headers properly but this error appears:


Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\polis\codes.php:1) in C:\AppServ\www\polis\codes.php on line 99
I fixed the "Cannot modify header information" error. I take out a BOM signature and the error stop appearing, but the strings are still cut wherever a spanish character appears.
Please post one of the failing strings here, in the code snippet.  EE uses UTF-8.  So does JSON.  See the notes here:
http://us3.php.net/manual/en/function.json-encode.php
Where it says "Espa" it should appear España, but it's cut off. Another thing is the URL slashes, they appear escaped. These is part of the resultant JSON string:
[{"ItmCod":"10","ItmName":"Centro Cultural de Espa","ItmDescription":"El Centro Cultural de Espa","ItCIconURL":"http:\/\/google-maps-icons.googlecode.com\/files\/dancinghall.png","ItSIconURL":null}]

Open in new window

I also noticed something weird. In Google Chrome the JSON displays itself, wrong but it displays. Using Mozilla Firefox the navigator invite me to download the resultant file. That should not happen.
Can you show us the data from the data base before the json_encode() -- this might be helpful to see.
This seems to handle it correctly.  HTH, ~Ray
<?php // RAY_temp_ivangarcete.php
error_reporting(E_ALL);
echo "<pre>";

// TEXT WITH A SPANISH CHARACTER
$text = "España";
echo PHP_EOL . $text;

// URL-ENCODED FOR TRANSPORT
$code = urlEncode($text);
echo PHP_EOL . $code;

// JSON-ENCODED
$json = json_encode($code);
var_dump($json);

// DECODED
$back = json_decode($json);
var_dump($back);

// URL-DECODED
$mine = urlDecode($back);
echo PHP_EOL . $mine;

// SELF TEST
if ($text == $mine) echo PHP_EOL . 'SUCCESS!';

Open in new window

Can you show us the data from the data base before the json_encode() -- this might be helpful to see.
This is part of the database dump of the table:
INSERT INTO `item` (`ItmCod`, `ItmName`, `ItmDescription`, `ItCCod`, `ItSCod`, `ItmAddress`, `ItmCity`, `ItmCountry`, `ItmLatitude`, `ItmLongitude`, `ItmActive`) VALUES
  (1,'Fónoluz','Electrificadores de alambrados -  Alarmas residenciales -  Electricidad - Materiales -  Telefonia - Equipos y Accesorios -  Portones a control remoto - Porteros electricos -  Circuito cerrado de TV -  Video porteros')

In that case Fónoluz appears as F and everything else is cut.

The script you pass me get this result to me:
España
Espa%C3%B1astring(13) ""Espa%C3%B1a""
string(11) "Espa%C3%B1a"

España
SUCCESS!
When I get it like this it works. But I would still have the same problem with the strins beacause I retrived from a JavaScript AJAX call. Probably if I can get a utf8 decode function in JavaScript it would theoretically work. Any thoughts? Am I doing something wrong?
// TEXT WITH A SPANISH CHARACTER
$text = "España";
echo PHP_EOL . utf8_decode($text);

Open in new window

instead using ut8_decode use utf8_encode :

while($r = mysql_fetch_assoc($rsItem)) {
    $rows[] = array('ItmCod'=>utf8_encode($r->ItmCod), 'ItmName'=>utf8_encode($r->ItmName), 'ItmDescription'=>utf8_encode($r->ItmDescription));
}

Open in new window

leakim971, something else happened to the spanish characters now. When I use your code I'm getting this:
Now España looks like this: Espa\u00f1a

 
[{"ItmCod":"10","ItmName":"Centro Cultural de Espa\u00f1a Juan de Salazar - AECID","ItmDescription":"El Centro Cultural de Espa\u00f1a Juan de Salazar, CCEJS, es una instituci\u00f3n dependiente de la Embajada de Espa\u00f1a en Paraguay. Est\u00e1 adscrito a la Direcci\u00f3n de Relaciones Culturales y Cient\u00edficas (DRCC) de la Agencia Espa\u00f1ola de Cooperaci\u00f3n Internacional para el Desarrollo (AECID)."}

Open in new window


I also try utf8_decode, but it didn't work either.
Please go back to ID:34226513.

Install that script on your server and run it, then tell us if it works, and post the output here.  Thank you, ~Ray
Ray, I've already did that, and I transcribe the result. This is what I wrote:

The script you pass me get this result to me:
España
Espa%C3%B1astring(13) ""Espa%C3%B1a""
string(11) "Espa%C3%B1a"

España
SUCCESS!

and then I wrote:

When I get it like this it works. But I would still have the same problem with the strins beacause I retrived from a JavaScript AJAX call. Probably if I can get a utf8 decode function in JavaScript it would theoretically work. Any thoughts? Am I doing something wrong?
// TEXT WITH A SPANISH CHARACTER
$text = "España";
echo PHP_EOL . utf8_decode($text);

Open in new window

Do you have a live link?
leakim971, I haven't upload it. It's a local project for now.
What happened when you tried my code ID:34226778 ?
leakim971, when I tried your code España looks like this: Espa\u00f1a. I have write about this in ID: 34226898
España has no UTF-8 characters.  The little ñ is decimal 241.  So if your computer is turning it into España it would appear that you have local settings that are confused.  UTF-8 has some multi-byte characters, but ñ is not one of them.  

You can test the program I posted above using your browser.  Here is the link:
http://www.laprbass.com/RAY_temp_ivangarcete.php

Please try it and then post back to tell us what you see.  thanks, ~Ray
Decimal 241 in ISO-8859-1 you mean, Ray?
Yes, in "ansi" characters according to my trusty copy of Textpad.
Ray, I tried on my server, but it didn't work. You can see it here: http://blue-ware.net/utf8.php.

But unexpectedly the first thing leakim971 told me is now working, I don't know why, I'm sure I tried it and it didn't work.

header('Content-type: application/json; charset=utf8'); // <- This works
http://blue-ware.net/utf8.php
I don't understand why you say this did not work.  It did EXACTLY what it was supposed to do - namely demonstrate that urlencode() makes the text safe for transport via JSON.  This is exactly the expected output.  It is the same output as I got on my system.  You can test it here: http://www.laprbass.com/RAY_temp_ivangarcete.php

España
Espa%C3%B1astring(13) ""Espa%C3%B1a""
string(11) "Espa%C3%B1a"

España
SUCCESS!

Check the man page here: http://us.php.net/manual/en/function.urlencode.php
Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
I don't understand. You're result it's different then mine. I see this:

España
Espa%C3%B1astring(13) ""Espa%C3%B1a""
string(11) "Espa%C3%B1a"

España
SUCCESS!

As you can see España is España there. That's my local and remote result. Are you saying you see a different output even on my site?
I see it working on your site, but not in mine.