Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

€ character not being displayed in UTF-8 file

Posted on 2012-09-18
11
Medium Priority
?
367 Views
Last Modified: 2013-01-17
I am storing the euro symbol, € (Alt+0128), and some text in a mysql table. I am then using a select statement to grab this field and write it and some other data to a text file. I want the text file in unicode so I am converting the string from the mysql table to unicode using mb_convert_encoding. The code that converts the string is below.

<?php
$f = fopen ("datafile.csv",'w');
$output = mb_convert_encoding($input, 'UTF-8'); 
fputs($f, $output);
?>

Open in new window


When I open datafile.csv in Notepad++ the euro symbol (€) is being displayed as a blank. Does anyone know what would be causing this? Thanks in advance.
0
Comment
Question by:KOvitt
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38412631
did you save that file as utf-8 support? see the attachment
utf8.JPG
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38413012
> € (Alt+0128),
this is not utf-8 but some proprietary windows (CP-1252 ?)
in utf-8 it is 0x20ac
0
 

Author Comment

by:KOvitt
ID: 38413891
How can I insert the utf-8 code for €  into mysql table?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38413994
If you want to use UTF-8 you have to be 100% consistent in your encoding throughout the process, from the creation of the data, storing, transmitting, inserting into the data base, retrieving, and displaying on a web page.  This article will tell you some of the basics about character encoding.

http://www.joelonsoftware.com/articles/Unicode.html

The likely problem you've go there is that the original text editor or form input did what ahoffman described, putting in the hex 80 value for the Euro.  That works as an ANSI character, but not with UTF-8 strings.
0
 

Author Comment

by:KOvitt
ID: 38414386
How do you insert utf-8 characters into a database?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38414448
Which UTF-8 characters do you need?  I am asking that as a slightly rhetorical question because I often see clients who think they need UTF-8 but really need ISO-8859-1 or similar western character set encoding.  If you can tell us a little more about the languages you need to support, we might find a good answer for you (either with or without UTF-8).
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38415147
> How can I insert the utf-8 code for €  into mysql table?

first you have to configure your database with the proper character set, in mysql something like (please read your documentation first for some traps):
  CREATE DATABASE db CHARACTER SET utf8 COLLATE utf8;

then your application has to convert the character to valid utf8, in your case
  0x080 to 0x20ac
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38416417
try to store like &euro;
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38418319
dooh, why storing data encoded for a special markup?
IMHO that's always a bad idea and often becomes a security nightmare ...
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38418368
You may not need to store it as an entity, but rendering it as an entity seems to make sense to me.
http://www.laprbass.com/RAY_utf8_euro.php
<?php // RAY_utf8_euro.php
error_reporting(E_ALL);

// CREATE VARIABLES FOR OUR HTML
$abc = "This is the Euro symbol '€' ";
$def = iconv("ISO-8859-1", "UTF-8//TRANSLIT", $abc);
$ghi = "Another Euro symbol: " . pack('n*', 0x20ac);
$jkl = 'This one should work: &euro;';


// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<title>The Euro Symbol in UTF-8 Encoding</title>
</head>
<body>
<p>$abc</p>
<p>$def</p>
<p>$ghi</p>
<p>$jkl</p>
</body>
</html>
HTML5;

// RENDER THE WEB PAGE
echo $htm;

Open in new window

Best to all, ~Ray
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38419433
agrred with Ray: rendering as HTML Entity if the other side of the trust boubdary is a (web-) browser
but keep in mind that ISO-8859-1 dos not contain €, just ISO-8859-15

note that you have 2 boundaries for incoming and for outgoing:

incoming: browser ---> your.php ---> mysql
outgoing: browser <--- your.php <--- mysql
    trust boundary ---^-----------------^---------
you have to use different en-/decodings on each
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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