Avatar of SheppardDigital
SheppardDigital

asked on 

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?
PHP

Avatar of undefined
Last Comment
Chris Stanyon
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of SheppardDigital
SheppardDigital

ASKER

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?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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

Avatar of SheppardDigital
SheppardDigital

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SheppardDigital
SheppardDigital

ASKER

Thanks Ray, that explains in perfectly.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Thanks for the points...
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo