json_encode outputting pound sign as \u00a3

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?
SheppardDigitalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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
Ray PaseurCommented:
0
SheppardDigitalAuthor Commented:
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
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Dave BaldwinFixer of ProblemsCommented:
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
SheppardDigitalAuthor Commented:
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
Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SheppardDigitalAuthor Commented:
Thanks Ray, that explains in perfectly.
0
Dave BaldwinFixer of ProblemsCommented:
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
Chris StanyonWebDevCommented:
I know Ray posts a lot of answers, but I'm not Ray - I'm Chris :)

Thanks for the points...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.