473,652 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accented characters getting corrupted when parsing CSV file

I am writing a script that allows people to upload a CSV file for
processing and I am having trouble with accented characters. If a
UTF-8 encoded file is uploaded, everything works fine. The problem
happens when someone uploads a non UTF8 file that happens to have an
accented character inside, like é. In this case, the character is
converted to a funky symbol, like �.

I noticed this explanation in the PHP documentation for the "fgetcsv"
that I am using to read the file: "Note: Locale setting is taken into
account by this function. If LANG is e.g. en_US.UTF-8, files in one-
byte encoding are read wrong by this function."

So my question is, how do I get around this? My script needs to be
able to handle a UTF8 file and a non-UTF8 file with accents. It seems
like I can only pick one. Anyone know of a way to convert the funky
symbol back to the accented character?

Thanks
Oct 21 '08 #1
3 7468
Here is a simple way to reproduce the problem.

1) Create an empty file with this string inside "a ". Save this
with ISO-8859-1 encoding.
2) Create a PHP page that has this inside: echo
file_get_conten ts("myfile.txt" );

The output from that will be gobbledygook. If you resave the file
with UTF-8 encoding, it will work fine. I can't control how people
are encoding their files so I need to be able to handle both cases.

I've tried using mb_convert_enco ding and utf8_decode without luck.
Oct 21 '08 #2
*** Jake escribió/wrote (Tue, 21 Oct 2008 12:05:03 -0700 (PDT)):
I am writing a script that allows people to upload a CSV file for
processing and I am having trouble with accented characters. If a
UTF-8 encoded file is uploaded, everything works fine. The problem
happens when someone uploads a non UTF8 file that happens to have an
accented character inside, like é. In this case, the character is
converted to a funky symbol, like �.
You can use iconv() to convert between charsets but you must know what
charset the CSV is using.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Oct 22 '08 #3
Álvaro G. Vicario wrote:
*** Jake escribió/wrote (Tue, 21 Oct 2008 12:05:03 -0700 (PDT)):
>I am writing a script that allows people to upload a CSV file for
processing and I am having trouble with accented characters. If a
UTF-8 encoded file is uploaded, everything works fine. The problem
happens when someone uploads a non UTF8 file that happens to have an
accented character inside, like é. In this case, the character is
converted to a funky symbol, like �.

You can use iconv() to convert between charsets but you must know what
charset the CSV is using.
Also, a couple functions to look up for detecting and converting encoding:

<URL:http://php.net/manual/en/function.mb-detect-encoding.php>
<URL:http://php.net/manual/en/function.mb-convert-encoding.php>

--
Curtis
Oct 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3836
by: Bernhard Georg Enders | last post by:
I'm using the php 'file' command to read the contents of an ASCII text file to a variable. The original text file contains some accented and special characters. The problem arises when I echo this variable: the accented characters are messed up. And not only accented characters, for example, the special characters is printed as ª. What can I do to correct this issue? I have tried, without success, to use the 'setlocale' command. TIA,
1
2444
by: Fuzzyman | last post by:
I've written an anagram finder that produces anagrams from a dictionary of words. The user can load their own dictionary. ( http://www.voidspace.org.uk/atlantibots/nanagram.html ) In order to ensure it is able to find anagrams properly I wanted to strip characters like punctuation etc from words in the dictionary and words the user entered. I test(ed) against the 26 English letters ( string.ascii_lowercase ).
14
16056
by: Nicolas Bouillon | last post by:
Hi I would like to replace accentuel chars (like "é", "è" or "à") with non accetued ones ("é" -> "e", "è" -> "e", "à" -> "a"). I have tried string.replace method, but it seems dislike non ascii chars... Can you help me please ? Thanks.
10
4512
by: John Byrne | last post by:
I have encountered a problem when certain European characters are included in the Querystring of an URL. If I have a URL like: ?param=AAà On the receiving page a simple Request() call returns "AA" and not "AAà". It drops all accented characters! If I change to use a Form instead, it works OK.
4
2027
by: Satish | last post by:
Hi Gurus, Please help me in this, I have tried all options available to me. (Option 1) I am making a simple request from VB.NET client to WBI generated WSDL and passing request parameters. The response I am expecting should have some accented characters (Customer Name: Lokalcenter Åbygård), but this gets truncated and response is as follows (Customer Name: Lokalcenter bygrd). The WSDL has utf-8 character set defined.
7
2565
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to xml files, but it is based on asap2 standard). i created the parser using bison and flex. then ported the code to vc+ + environment including the files lex.yy.c(output from the flex scanner) a2lgrammar.tab.cpp(output from bison) for post...
0
2296
by: shintu | last post by:
Hallo, I am trying to write french accented characters in Excel worksheet using my perl script , But I am stuck here as I couldnt find a way of writing it !: My code: use strict; use warnings;
4
2885
by: gsuns82 | last post by:
Hi all, I am facing a strange issue. i.e: I have a jsp page with an input text field where the user can enter searching value even along with Accented Characters.After that i am getting the input value at the controller inorder to compose a query,before that i am replacing Accented Characters with plain text values so that i can append the proper input value in the DBquery. I tried to replace the input string for ...
1
1977
by: gsauns | last post by:
I have an ASP.NET app in which I import from a comma-delimited text file, put all that data in a GridView, and then insert the records into multiple related tables in my SQL Server database. I got one text file which contains accented characters. When I bring them into the GridView, the accented characters show up as boxes, the universal unrecognized character. And since I am inserting into the DB by iterating thru the rows of the...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.