473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to determine if a file is UTF8 encoded?

I need a check, if a file is utf8 encoded. I only found the php-functions
'iconv' and 'recode'. But it seems it´s not possible to determine the
encoding with them. Isn´t there any similar function to the 'file'-command
on linux for php?
Nov 22 '05 #1
9 24174
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Podlesak wrote:
I need a check, if a file is utf8 encoded. I only found the php-functions
'iconv' and 'recode'. But it seems it´s not possible to determine the
encoding with them. Isn´t there any similar function to the 'file'-command
on linux for php?


Ok, I'm barly understanding your request. UTF/UTF8/UTF16 affects the
character set of your file. Now all HTML files can configure the browser
to determin the character set just be defining:

<meta http-equiv="Content-Type" content="application/xml+xhtml;
charset=UTF-8" />

As well, if your sending PHP encoded file, you can pre-determin the
filetype just by defining the character encoding threw the headers:

header("Content-Type: application/xml+xhtml; charset=utf-8");

Other then that, the rest is done threw your web server, picking the
file... that if you wanted to encode it further you would have to
convert it using the functions you said above.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDfmPQ/WE0aXnOUiYRArvoAJ4tE6s7WPSRtBZg1tLdFgJmFL5MNQCbBKU e
GR3X5SE21hzNzW4k2UOh7f8=
=xJ0s
-----END PGP SIGNATURE-----
Nov 22 '05 #2
That's not my problem, daemon.

The Problem is: The client uploads a csv-file. The php-script has to ensure
that the uploaded file is utf-8 encoded.

daemon wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Podlesak wrote:
I need a check, if a file is utf8 encoded. I only found the php-functions
'iconv' and 'recode'. But it seems it´s not possible to determine the
encoding with them. Isn´t there any similar function to the
'file'-command on linux for php?


Ok, I'm barly understanding your request. UTF/UTF8/UTF16 affects the
character set of your file. Now all HTML files can configure the browser
to determin the character set just be defining:

<meta http-equiv="Content-Type" content="application/xml+xhtml;
charset=UTF-8" />

As well, if your sending PHP encoded file, you can pre-determin the
filetype just by defining the character encoding threw the headers:

header("Content-Type: application/xml+xhtml; charset=utf-8");

Other then that, the rest is done threw your web server, picking the
file... that if you wanted to encode it further you would have to
convert it using the functions you said above.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDfmPQ/WE0aXnOUiYRArvoAJ4tE6s7WPSRtBZg1tLdFgJmFL5MNQCbBKU e
GR3X5SE21hzNzW4k2UOh7f8=
=xJ0s
-----END PGP SIGNATURE-----


Nov 22 '05 #3
"Thomas Podlesak" wrote:
I need a check, if a file is utf8 encoded. I only found the php-functions
'iconv' and 'recode'. But it seems it´s not possible to determine the
encoding with them. Isn´t there any similar function to the 'file'-command
on linux for php?


Try this: <http://php.net/mb-detect-encoding> (but make sure you read the
notes at the bottom, especially <http://php.net/mb-detect-encoding#50087>).

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Nov 22 '05 #4
Try this:

$text = file_get_contents("test.txt");
echo preg_match('/./u', $text);

The u modifier tell PCRE the input is UTF-8. If it's not properly
encoded, then it'll return false.

Nov 22 '05 #5
Chung Leong wrote:
echo preg_match('/./u', $text);


That will match on any single utf8 character, which could potentially be
followed by non-utf8 data... Also, I'm not sure about its behaviour when
encountering such data.

--
E. Dronkert
Nov 22 '05 #6

Ewoud Dronkert wrote:
Chung Leong wrote:
echo preg_match('/./u', $text);


That will match on any single utf8 character, which could potentially be
followed by non-utf8 data... Also, I'm not sure about its behaviour when
encountering such data.

--
E. Dronkert


PCRE validates the string before it runs the expression.

pcre.c:8037
if (valid_utf8((uschar *)subject, length) >= 0)
return PCRE_ERROR_BADUTF8;

If it isn't valid all the way through, then there's no match.

Nov 22 '05 #7
Chung Leong wrote:
PCRE validates the string before it runs the expression.
If it isn't valid all the way through, then there's no match.


OK, but aren't charsets like latin1 (8859-1) subsets of utf8, and us-ascii
of them? So those would also be considered utf8.

--
E. Dronkert
Nov 22 '05 #8
Ewoud Dronkert wrote:
Chung Leong wrote:
PCRE validates the string before it runs the expression.
If it isn't valid all the way through, then there's no match.


OK, but aren't charsets like latin1 (8859-1) subsets of utf8, and us-ascii
of them? So those would also be considered utf8.


The Latin 1 is a subset of Unicode, true enough, with matching
codepoints. But when encoded as UTF-8, characters in the U+00F0 -
U+00FF will become 2 byte sequences. So text in 8859-1 with curly
quotes and such won't be identified as UTF-8. Text with characters only
in the basic Latin range (i.e. ASCII) would be identical to UTF-8.

It's of course possible to construct a text encoded in 8859-1, KOI8-R,
or whatever, that would appear as valid UTF-8. It'd be total gibberish
though. In a UTF-8 byte sequence, a byte with bit-6 on has to be
followed by a byte with bit-6 off. In a 8-bit charset, that means a
separation of at least 32 code points--too far apart to stay within the
alphabet.

Nov 22 '05 #9
In a other group somebody recommended the PECL Fileinfo extension.
http://pecl.php.net/package/Fileinfo

Seems ok for me.

Thanks for your help!
Thomas
Nov 22 '05 #10

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

Similar topics

21
by: Sami Viitanen | last post by:
Hello, How can I check if a file is binary or text? There was some easy way but I forgot it.. Thanks in adv.
1
by: Asra | last post by:
Hello, I have a frame's onload event calling a function which needs to know which file was loaded. Is there any java object/method to determine the file name or any type of file id? Thanks, Asra
2
by: Mark Anderson | last post by:
Hi, I've a problem with code that should produce a Windows(ANSI) encoded text file but doesn't. Server is IIS 5 on Win 2k, with ASP ver? My ASP uses data from an upstream HTML form on a UTF-8...
10
by: Marc Jennings | last post by:
Hi there, Can anyone point out any really obvious flaws in the methodology below to determine the likely encoding of a file, please? I know the number of types of encoding is small, but that is...
8
by: Dinesh Jain | last post by:
Hi all, I encountered a serious problem while working with clipboard class in VB.NET. I want to simulate cut-copy-paste operation with my application and Windows Explorer. User can copy files...
3
by: Mark Gibson | last post by:
Is there an equivalent to the unix 'file' command? $ file min.txt min.txt: ASCII text $ file trunk trunk: directory $ file compliance.tgz compliance.tgz: gzip compressed data, from Unix ...
1
by: nalinibala | last post by:
I'm having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "human unreadable" characters show up *right before* the XML declaration. I need to have the XML...
0
by: Tim Golden | last post by:
Lawrence, Anna K (US SSA) wrote: From where I'm sitting, I can't see enough to help. The crucial thing seems to be in this phrase: "When I try to take a string that is coming out of the database...
4
by: Samuel | last post by:
Hi I am trying to write to a string text encoded to utf8 as oppose to utf16 Since the data comes from an XML object (and I serialize it) I need to pass either StreamWriter or a StringWriter...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.