encoding [message #186326] |
Thu, 26 June 2014 05:50 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Tear my hair out time.
I have a csv file that contains text strings that I wish to display in a
web page.
The csv file is utf-8, and the text strings include the british pound
symbol encoded as two bytes 0xc2/0xa3
I'm using
setlocale(LC_CTYPE|LC_COLLATE,"en_GB.UTF8");
before reading the csv file, which I hope means that the csv file is read
as utf-8.
Then I feed the string through htmlentities() before adding it to the web
page.
However, the web page that arrives at the client has £
instead of just £.
I'm not sure where it's going wrong, partly because right now I may be
too tired to work out where and how I can inspect the string without
character encodings getting in the way.
If I print_r the data that has been read in to the web page, that shows
ok, but at that point it's still utf-8, not an html entity.
The following is at http://www.sined.co.uk/tmp/pound.php and seems to
demonstrate the issue:
<?php
setlocale(LC_CTYPE|LC_COLLATE, "en_GB.UTF8");
$str1 = "\xc2\xa3";
$str2 = htmlentities( "$str1" );
echo <<< EOT
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Broken Pound</title>
<head>
<body>
<pre>
Original: {$str1}
Entities: {$str2}
</pre>
</body>
</html>
EOT;
I'm not sure how to fix this. Ideas anyone?
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: encoding [message #186327 is a reply to message #186326] |
Thu, 26 June 2014 07:04 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Thu, 26 Jun 2014 05:50:06 +0000, Denis McMahon wrote:
> I'm not sure how to fix this. Ideas anyone?
Fix was:
htmlentities( $string, ENT_COMPAT, "UTF-8" );
Not sure if I actually need the setlocale or not. Seems to work without
it.
ENT_HTML5 isn't supported in my server distro's current php (5.3) ...
mutter mutter
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: encoding [message #186328 is a reply to message #186327] |
Thu, 26 June 2014 11:20 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma: 0
|
Senior Member |
|
|
Denis McMahon wrote:
> htmlentities( $string, ENT_COMPAT, "UTF-8" );
>
> Not sure if I actually need the setlocale or not. Seems to work without
> it.
In PHP < 5.4 the default of the 3rd parameter is 'ISO-8859-1', so
setting this parameter appropriately is important when $string may
contain non ASCII characters. For instance:
htmlentities("\xC3\xA4", ENT_COMPAT, 'ISO-8859-1');
// => 'ä'
htmlentities("\xC3\xA4", ENT_COMPAT, 'UTF-8');
// => 'ä'
--
Christoph M. Becker
|
|
|