Solved

JSON string with incorrect spanish characters

Posted on 2010-11-28
25
1,775 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:IvanGarcete
  • 13
  • 7
  • 4
  • +1
25 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34226224
Could you confirm your database charset is utf8 ?
Else : http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html

You may try this :

header('Content-type: application/json; charset=utf8');
print json_encode($rows);
mysql_free_result($rsItem);
?>

Open in new window

0
 

Author Comment

by:IvanGarcete
ID: 34226297
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
0
 

Author Comment

by:IvanGarcete
ID: 34226423
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34226435
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
0
 

Author Comment

by:IvanGarcete
ID: 34226452
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

0
 

Author Comment

by:IvanGarcete
ID: 34226463
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34226466
Can you show us the data from the data base before the json_encode() -- this might be helpful to see.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34226513
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

0
 

Author Comment

by:IvanGarcete
ID: 34226578
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!
0
 

Author Comment

by:IvanGarcete
ID: 34226623
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

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34226778
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

0
 

Author Comment

by:IvanGarcete
ID: 34226898
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34226956
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
0
 

Author Comment

by:IvanGarcete
ID: 34226966
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

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34227045
Do you have a live link?
0
 

Author Comment

by:IvanGarcete
ID: 34227068
leakim971, I haven't upload it. It's a local project for now.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34227073
What happened when you tried my code ID:34226778 ?
0
 

Author Comment

by:IvanGarcete
ID: 34227110
leakim971, when I tried your code España looks like this: Espa\u00f1a. I have write about this in ID: 34226898
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34228512
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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34229010
Decimal 241 in ISO-8859-1 you mean, Ray?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34229262
Yes, in "ansi" characters according to my trusty copy of Textpad.
0
 

Author Comment

by:IvanGarcete
ID: 34230071
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34230509
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.
0
 

Author Comment

by:IvanGarcete
ID: 34230543
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?
0
 

Author Comment

by:IvanGarcete
ID: 34230554
I see it working on your site, but not in mine.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now