• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

€ character not being displayed in UTF-8 file

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
KOvitt
Asked:
KOvitt
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
did you save that file as utf-8 support? see the attachment
utf8.JPG
0
 
ahoffmannCommented:
> € (Alt+0128),
this is not utf-8 but some proprietary windows (CP-1252 ?)
in utf-8 it is 0x20ac
0
 
KOvittAuthor Commented:
How can I insert the utf-8 code for €  into mysql table?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ray PaseurCommented:
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
 
KOvittAuthor Commented:
How do you insert utf-8 characters into a database?
0
 
Ray PaseurCommented:
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
 
ahoffmannCommented:
> 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
 
Loganathan NatarajanLAMP DeveloperCommented:
try to store like &euro;
0
 
ahoffmannCommented:
dooh, why storing data encoded for a special markup?
IMHO that's always a bad idea and often becomes a security nightmare ...
0
 
Ray PaseurCommented:
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
 
ahoffmannCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: 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.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now