Solved

json_encode outputting pound sign as \u00a3

Posted on 2014-12-15
9
757 Views
Last Modified: 2014-12-16
I'm performing a query on a database and encoding the results to json via json_encode.

The issue is that a pound sign £ is being outputted to the browser as '\u00a3'

Here's how I'm sending the json to the browser.

header('Content-Type: application/json; charset=utf-8;');
echo json_encode($this->response);

Open in new window


Can someone tell me what's going wrong?
0
Comment
Question by:SheppardDigital
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40500777
Nothing's wrong - you've set your encoding to UTF-8 which encodes the pound sign as u00a3 - it's working exactly as it should. If the page you're calling it from is also set to UTF8 then when you echo out the JSON result, you'll see the proper £ sign.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40501765
0
 

Author Comment

by:SheppardDigital
ID: 40502088
Thanks.

What I don't understand is why the pound sign isn't rendering in the browser when I'm passing over a head to the browser to tell it the document is UTF-8?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40502182
Simple test page works in Firefox 34, Chrome, IE8, and Safari 5.  Note the UTF-8 declaration in the page.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JS Unicode Test</title>
</head>
<body>
<h1>JS Unicode Test</h1>
<script type="text/javascript">
<!--
document.write("This is \u00a3 .");
// -->
</script>
</body>
</html>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:SheppardDigital
ID: 40502184
Hi Dave,

I'm outputting just json to the browser, so that it can be used by other applications so we can't wrap it in HTML tags.

This isn't really an issue now, as the mobile app developer who is working with the API confirms that iOS handles the UTF-8 fine.

Why it doesn't render properly in my browser I don't know.
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40502202
If you just call your script in a browser it will show the UTF codes - it's not until you call it in Javascript that you'll see the £ sign. It sounds to me like it's working exactly as it should.

Try calling your JSON page using javascript and alerting the result - you'll see it's fine. Create an HTML page that includes jQuery and use something like the following:

$.getJSON( "yourScript.php", function( data ) {
   alert(data.yourValue);
});

Open in new window


Substitue yourScript.php with the name of your php file and yourValue with whatever you've called the array key in your php file.
0
 

Author Comment

by:SheppardDigital
ID: 40502257
Thanks Ray, that explains in perfectly.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40502262
I'm outputting just json to the browser
There's your problem then.  JSON is not valid HTML and if there is nothing on the page to support it like on my demo, it will be rendered in plain text with no recognition of UTF-8.

While I have pages that do send non-HTML data, they are not being sent to browsers but to other pages on servers that know what to do with them.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40502268
I know Ray posts a lot of answers, but I'm not Ray - I'm Chris :)

Thanks for the points...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

12 Experts available now in Live!

Get 1:1 Help Now