FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Displaying UTF-8-encoded strings from MySQL with PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Displaying UTF-8-encoded strings from MySQL with PHP [message #174508] Wed, 15 June 2011 10:18 Go to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
Hi there,

I'm trying to migrate my project from ISO-8859-1 (Western Europe) to
UTF-8. I *can* write special characters as regular text in my php-
files, I can use special characters in echo-statements and I can even
use special characters in .ini-files, load them, and display them. But
when I load Strings with special characters from MySQL and display
them, the string's "broken".

I've tried many things: First of all, I use

<?xml version="1.0" encoding="utf-8" ?>
and
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

in the markup. In the database I tried to set my table's collation to
"utf8_general_ci". Also I tried the following functions:

mysql_query("SET NAMES ‘utf8′");
mysql_set_charset('utf8');
mb_internal_encoding("UTF-8");

all not at the very beginning of my script, but before I queried the
database and before I printed the results. I still keep getting
'wierd' characters.

Does anyone have an idea what I can do?

Thanks,
Lukas
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174509 is a reply to message #174508] Wed, 15 June 2011 10:26 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 15/06/2011 12:18, Luke escribió/wrote:
> I'm trying to migrate my project from ISO-8859-1 (Western Europe) to
> UTF-8. I *can* write special characters as regular text in my php-
> files, I can use special characters in echo-statements and I can even
> use special characters in .ini-files, load them, and display them. But
> when I load Strings with special characters from MySQL and display
> them, the string's "broken".
>
> I've tried many things: First of all, I use
>
> <?xml version="1.0" encoding="utf-8" ?>
> and
> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
>
> in the markup. In the database I tried to set my table's collation to
> "utf8_general_ci". Also I tried the following functions:
>
> mysql_query("SET NAMES ‘utf8′");
> mysql_set_charset('utf8');
> mb_internal_encoding("UTF-8");
>
> all not at the very beginning of my script, but before I queried the
> database and before I printed the results. I still keep getting
> 'wierd' characters.
>
> Does anyone have an idea what I can do?

Here's quite a complete list of the required steps:

http://www.itnewb.com/v/UTF-8-Enabled-Apache-MySQL-PHP-Markup-and-JavaScrip t

Whatever, are you sure that your data is actually stored as valid UTF-8
after the migration?

The HEX() function can be handy:

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_hex



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174511 is a reply to message #174509] Wed, 15 June 2011 10:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/15/2011 6:26 AM, "Álvaro G. Vicario" wrote:
> El 15/06/2011 12:18, Luke escribió/wrote:
>> I'm trying to migrate my project from ISO-8859-1 (Western Europe) to
>> UTF-8. I *can* write special characters as regular text in my php-
>> files, I can use special characters in echo-statements and I can even
>> use special characters in .ini-files, load them, and display them. But
>> when I load Strings with special characters from MySQL and display
>> them, the string's "broken".
>>
>> I've tried many things: First of all, I use
>>
>> <?xml version="1.0" encoding="utf-8" ?>
>> and
>> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
>>
>> in the markup. In the database I tried to set my table's collation to
>> "utf8_general_ci". Also I tried the following functions:
>>
>> mysql_query("SET NAMES ‘utf8′");
>> mysql_set_charset('utf8');
>> mb_internal_encoding("UTF-8");
>>
>> all not at the very beginning of my script, but before I queried the
>> database and before I printed the results. I still keep getting
>> 'wierd' characters.
>>
>> Does anyone have an idea what I can do?
>
> Here's quite a complete list of the required steps:
>
> http://www.itnewb.com/v/UTF-8-Enabled-Apache-MySQL-PHP-Markup-and-JavaScrip t
>
>
> Whatever, are you sure that your data is actually stored as valid UTF-8
> after the migration?
>
> The HEX() function can be handy:
>
> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_hex
>
>
>

Álvaro, I admit I'm not great at UTF-8. This is a great guide if you
have your own server and want everything in UTF-8. However, do you know
of a similar reference for those who use shared servers and/or don't
want everything to default to UTF-8?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174512 is a reply to message #174511] Wed, 15 June 2011 11:57 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 15/06/2011 12:53, Jerry Stuckle escribió/wrote:
>> http://www.itnewb.com/v/UTF-8-Enabled-Apache-MySQL-PHP-Markup-and-JavaScrip t
>>
>>
>>
>> Whatever, are you sure that your data is actually stored as valid UTF-8
>> after the migration?
>>
>> The HEX() function can be handy:
>>
>> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_hex
>>
>>
>>
>
> Álvaro, I admit I'm not great at UTF-8. This is a great guide if you
> have your own server and want everything in UTF-8. However, do you know
> of a similar reference for those who use shared servers and/or don't
> want everything to default to UTF-8?

I suppose you refer to the MySQL section (the rest of the stuff is easy
to customize). I believe you can safely ignore it: it's only a trick to
use UTF-8 with misconfigured clients. All you have to do is, well,
configure your client properly. From my experience, this is all you need
in PHP:


// Good old MySQL extension
mysql_set_charset('utf8', $conn)


// PDO
$pdo_options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$conn = new PDO(..., $pdo_options);


With mb_convert_encoding() you can retrieve and store UTF-8 data even if
your app does not use it ;-)


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174516 is a reply to message #174509] Wed, 15 June 2011 12:56 Go to previous messageGo to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
> Whatever, are you sure that your data is actually stored as valid UTF-8
> after the migration?
>
> The HEX() function can be handy:
>
> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function...

Hm.. getting closer. But this is very weird:


// Simply performs a query and returns mysql_fetch_assoc on the first
result
$hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
id=2");

// Echos "C3BC" (which is the correct value for the character in the
DB ("ü"))
echo $hex['HEX(link_text)'];

// Selecting the actual value instead of its hex-represenation
$char = $theDB->getResult("SELECT link_text FROM language WHERE
id=2");

// Echos "fc" (which is "�" (broken character))
echo bin2hex($char['link_text']);


I'm a little confused. I tried various functions on this:

$theDB->query("SET NAMES ‘utf8′");
mysql_set_charset('utf8');
mb_internal_encoding("UTF-8");

mb_internal_encoding("UTF-8");

Nothing helps. Changing configurations is not impossible, but very
difficult. Also the database seems fine, as HEX(xx) returns the
correct value, and in my PHP-Scripts I can use UTF-8 characters (that
aren't loaded from the database) and they work too. There must be
something in 'between'…
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174517 is a reply to message #174516] Wed, 15 June 2011 13:05 Go to previous messageGo to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
On Jun 15, 2:56 pm, Luke <kickingje...@gmail.com> wrote:
>> Whatever, are you sure that your data is actually stored as valid UTF-8
>> after the migration?
>
>> The HEX() function can be handy:
>
>> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function...
>
> Hm.. getting closer. But this is very weird:
>
> // Simply performs a query and returns mysql_fetch_assoc on the first
> result
> $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
> id=2");
>
> // Echos "C3BC" (which is the correct value for the character in the
> DB ("ü"))
> echo $hex['HEX(link_text)'];
>
> // Selecting the actual value instead of its hex-represenation
> $char = $theDB->getResult("SELECT link_text FROM language WHERE
> id=2");
>
> // Echos "fc" (which is " " (broken character))
> echo bin2hex($char['link_text']);
>
> I'm a little confused. I tried various functions on this:
>
> $theDB->query("SET NAMES ‘utf8′");
> mysql_set_charset('utf8');
> mb_internal_encoding("UTF-8");
>
> mb_internal_encoding("UTF-8");
>
> Nothing helps. Changing configurations is not impossible, but very
> difficult. Also the database seems fine, as HEX(xx) returns the
> correct value, and in my PHP-Scripts I can use UTF-8 characters (that
> aren't loaded from the database) and they work too. There must be
> something in 'between'…

And it seems FC is the "Hex code point" of the character I'm looking
for. But I actually have no clue what to do. Wikipedia says Hex code
point marks the range in which characters are assigned to languages
(more or less), but i'm not sure why that is returned or even how to
handle this.
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174519 is a reply to message #174517] Wed, 15 June 2011 13:23 Go to previous messageGo to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
On Jun 15, 3:05 pm, Luke <kickingje...@gmail.com> wrote:
> On Jun 15, 2:56 pm, Luke <kickingje...@gmail.com> wrote:
>
>
>
>
>
>>> Whatever, are you sure that your data is actually stored as valid UTF-8
>>> after the migration?
>
>>> The HEX() function can be handy:
>
>>> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function....
>
>> Hm.. getting closer. But this is very weird:
>
>> // Simply performs a query and returns mysql_fetch_assoc on the first
>> result
>> $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
>> id=2");
>
>> // Echos "C3BC" (which is the correct value for the character in the
>> DB ("ü"))
>> echo $hex['HEX(link_text)'];
>
>> // Selecting the actual value instead of its hex-represenation
>> $char = $theDB->getResult("SELECT link_text FROM language WHERE
>> id=2");
>
>> // Echos "fc" (which is " " (broken character))
>> echo bin2hex($char['link_text']);
>
>> I'm a little confused. I tried various functions on this:
>
>> $theDB->query("SET NAMES ‘utf8′");
>> mysql_set_charset('utf8');
>> mb_internal_encoding("UTF-8");
>
>> mb_internal_encoding("UTF-8");
>
>> Nothing helps. Changing configurations is not impossible, but very
>> difficult. Also the database seems fine, as HEX(xx) returns the
>> correct value, and in my PHP-Scripts I can use UTF-8 characters (that
>> aren't loaded from the database) and they work too. There must be
>> something in 'between'…
>
> And it seems FC is the "Hex code point" of the character I'm looking
> for. But I actually have no clue what to do. Wikipedia says Hex code
> point marks the range in which characters are assigned to languages
> (more or less), but i'm not sure why that is returned or even how to
> handle this.

Oh wait FC is the ASCII-Value for ü. So the question is, why does PHP
recieve an ASCII-char if the MySQL-Function HEX() returns the correct
UTF-8 hexvalue?
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174520 is a reply to message #174519] Wed, 15 June 2011 13:50 Go to previous messageGo to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
On Jun 15, 3:23 pm, Luke <kickingje...@gmail.com> wrote:
> On Jun 15, 3:05 pm, Luke <kickingje...@gmail.com> wrote:
>
>
>
>
>
>> On Jun 15, 2:56 pm, Luke <kickingje...@gmail.com> wrote:
>
>>>> Whatever, are you sure that your data is actually stored as valid UTF-8
>>>> after the migration?
>
>>>> The HEX() function can be handy:
>
>>>> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function...
>
>>> Hm.. getting closer. But this is very weird:
>
>>> // Simply performs a query and returns mysql_fetch_assoc on the first
>>> result
>>> $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
>>> id=2");
>
>>> // Echos "C3BC" (which is the correct value for the character in the
>>> DB ("ü"))
>>> echo $hex['HEX(link_text)'];
>
>>> // Selecting the actual value instead of its hex-represenation
>>> $char = $theDB->getResult("SELECT link_text FROM language WHERE
>>> id=2");
>
>>> // Echos "fc" (which is " " (broken character))
>>> echo bin2hex($char['link_text']);
>
>>> I'm a little confused. I tried various functions on this:
>
>>> $theDB->query("SET NAMES ‘utf8′");
>>> mysql_set_charset('utf8');
>>> mb_internal_encoding("UTF-8");
>
>>> mb_internal_encoding("UTF-8");
>
>>> Nothing helps. Changing configurations is not impossible, but very
>>> difficult. Also the database seems fine, as HEX(xx) returns the
>>> correct value, and in my PHP-Scripts I can use UTF-8 characters (that
>>> aren't loaded from the database) and they work too. There must be
>>> something in 'between'…
>
>> And it seems FC is the "Hex code point" of the character I'm looking
>> for. But I actually have no clue what to do. Wikipedia says Hex code
>> point marks the range in which characters are assigned to languages
>> (more or less), but i'm not sure why that is returned or even how to
>> handle this.
>
> Oh wait FC is the ASCII-Value for ü. So the question is, why does PHP
> recieve an ASCII-char if the MySQL-Function HEX() returns the correct
> UTF-8 hexvalue?

Ok, I got it. As supposed here http://akrabat.com/php/utf8-php-and-mysql/
I had to query "SET NAMES UTF8;" right after I connected to the
database. No need to change tables or configuring Apache and PHP
(well, I had to insert meta-tags setting UTF-8 as charset, but apart
from that there was nothing else to do).

Still thanks.
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174521 is a reply to message #174520] Wed, 15 June 2011 14:17 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 15/06/2011 15:50, Luke escribió/wrote:
>>>> // Simply performs a query and returns mysql_fetch_assoc on the first
>>>> result
>>>> $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
>>>> id=2");
>>
>>>> // Echos "C3BC" (which is the correct value for the character in the
>>>> DB ("ü"))
>>>> echo $hex['HEX(link_text)'];
>>
>>>> // Selecting the actual value instead of its hex-represenation
>>>> $char = $theDB->getResult("SELECT link_text FROM language WHERE
>>>> id=2");
>>
>>>> // Echos "fc" (which is " " (broken character))
>>>> echo bin2hex($char['link_text']);
>>
>>>> I'm a little confused. I tried various functions on this:
>>
>>>> $theDB->query("SET NAMES ‘utf8′");
>>>> mysql_set_charset('utf8');
>>>> mb_internal_encoding("UTF-8");
>>
>>>> mb_internal_encoding("UTF-8");
>>
>>>> Nothing helps. Changing configurations is not impossible, but very
>>>> difficult. Also the database seems fine, as HEX(xx) returns the
>>>> correct value, and in my PHP-Scripts I can use UTF-8 characters (that
>>>> aren't loaded from the database) and they work too. There must be
>>>> something in 'between'…
>>
>>> And it seems FC is the "Hex code point" of the character I'm looking
>>> for. But I actually have no clue what to do. Wikipedia says Hex code
>>> point marks the range in which characters are assigned to languages
>>> (more or less), but i'm not sure why that is returned or even how to
>>> handle this.
>>
>> Oh wait FC is the ASCII-Value for ü. So the question is, why does PHP
>> recieve an ASCII-char if the MySQL-Function HEX() returns the correct
>> UTF-8 hexvalue?


ASCII is a 7-bit encoding that does not have non-English letters like
"ü". That FC you get is the ISO-8859-1 code. The data gets converted
from UTF-8 to ISO-8859-1 (or a similar charset).

> Ok, I got it. As supposed here http://akrabat.com/php/utf8-php-and-mysql/
> I had to query "SET NAMES UTF8;" right after I connected to the
> database. No need to change tables or configuring Apache and PHP

Hadn't you tried that already?

http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

> (well, I had to insert meta-tags setting UTF-8 as charset, but apart
> from that there was nothing else to do).

Meta-tags are not useful unless you save the page to disk from your
browser and open it later. You should either use header() to set the
Content-Type header or configure it via .htaccess.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174522 is a reply to message #174521] Wed, 15 June 2011 14:26 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <itaeu4$lgp$1(at)dont-email(dot)me>,
"輙varo G. Vicario" <alvaro(dot)NOSPAMTHANX(at)demogracia(dot)com(dot)invalid> wrote:

> El 15/06/2011 15:50, Luke escribi�/wrote:
>>>> > // Simply performs a query and returns mysql_fetch_assoc on the first
>>>> > result
>>>> > $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
>>>> > id=2");
>>>
>>>> > // Echos "C3BC" (which is the correct value for the character in the
>>>> > DB ("�"))
>>>> > echo $hex['HEX(link_text)'];
>>>
>>>> > // Selecting the actual value instead of its hex-represenation
>>>> > $char = $theDB->getResult("SELECT link_text FROM language WHERE
>>>> > id=2");
>>>
>>>> > // Echos "fc" (which is " " (broken character))
>>>> > echo bin2hex($char['link_text']);
>>>
>>>> > I'm a little confused. I tried various functions on this:
>>>
>>>> > $theDB->query("SET NAMES ヤutf8′");
>>>> > mysql_set_charset('utf8');
>>>> > mb_internal_encoding("UTF-8");
>>>
>>>> > mb_internal_encoding("UTF-8");
>>>
>>>> > Nothing helps. Changing configurations is not impossible, but very
>>>> > difficult. Also the database seems fine, as HEX(xx) returns the
>>>> > correct value, and in my PHP-Scripts I can use UTF-8 characters (that
>>>> > aren't loaded from the database) and they work too. There must be
>>>> > something in 'between'…
>>>
>>>> And it seems FC is the "Hex code point" of the character I'm looking
>>>> for. But I actually have no clue what to do. Wikipedia says Hex code
>>>> point marks the range in which characters are assigned to languages
>>>> (more or less), but i'm not sure why that is returned or even how to
>>>> handle this.
>>>
>>> Oh wait FC is the ASCII-Value for �. So the question is, why does PHP
>>> recieve an ASCII-char if the MySQL-Function HEX() returns the correct
>>> UTF-8 hexvalue?
>
>
> ASCII is a 7-bit encoding that does not have non-English letters like
> "�". That FC you get is the ISO-8859-1 code. The data gets converted
> from UTF-8 to ISO-8859-1 (or a similar charset).
>
>> Ok, I got it. As supposed here http://akrabat.com/php/utf8-php-and-mysql/
>> I had to query "SET NAMES UTF8;" right after I connected to the
>> database. No need to change tables or configuring Apache and PHP
>
> Hadn't you tried that already?
>
> http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
>
>> (well, I had to insert meta-tags setting UTF-8 as charset, but apart
>> from that there was nothing else to do).
>
> Meta-tags are not useful unless you save the page to disk from your
> browser and open it later. You should either use header() to set the
> Content-Type header or configure it via .htaccess.

Indeed. Doing this at the start of all my PHP scripts:

header ("Content-Type: text/plain; charset=utf-8");

fixed my char-set problems.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174526 is a reply to message #174512] Wed, 15 June 2011 16:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/15/2011 7:57 AM, "Álvaro G. Vicario" wrote:
> El 15/06/2011 12:53, Jerry Stuckle escribió/wrote:
>>> http://www.itnewb.com/v/UTF-8-Enabled-Apache-MySQL-PHP-Markup-and-JavaScrip t
>>>
>>>
>>>
>>>
>>> Whatever, are you sure that your data is actually stored as valid UTF-8
>>> after the migration?
>>>
>>> The HEX() function can be handy:
>>>
>>> http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_hex
>>>
>>>
>>>
>>>
>>
>> Álvaro, I admit I'm not great at UTF-8. This is a great guide if you
>> have your own server and want everything in UTF-8. However, do you know
>> of a similar reference for those who use shared servers and/or don't
>> want everything to default to UTF-8?
>
> I suppose you refer to the MySQL section (the rest of the stuff is easy
> to customize). I believe you can safely ignore it: it's only a trick to
> use UTF-8 with misconfigured clients. All you have to do is, well,
> configure your client properly. From my experience, this is all you need
> in PHP:
>

That's part of it, but even the PHP and Apache configurations can be
hard to duplicate in your code, and generally you need to do it in every
file.

Of course, you can use .htaccess to configure such, if the web server
allows. But I'm not even sure if some of those Apache settings are
valid in the .htaccess (haven't checked). And of course .htaccess isn't
available if you're running on IIS.

>
> // Good old MySQL extension
> mysql_set_charset('utf8', $conn)
>
>
> // PDO
> $pdo_options = array(
> PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
> );
> $conn = new PDO(..., $pdo_options);
>
>
> With mb_convert_encoding() you can retrieve and store UTF-8 data even if
> your app does not use it ;-)
>
>

How's mb_convert_encoding() working? Is it correct in converting
(assuming, of course, the character exists in the character set being
converted to).

I'm asking this because I only dabbled a bit in utf-8 several years ago,
and haven't touched it since (yea, we Americans think the whole world
revolves around us :) ). But I may soon be involved in a project which
does use it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174531 is a reply to message #174526] Thu, 16 June 2011 07:29 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 15/06/2011 18:07, Jerry Stuckle escribió/wrote:
>> With mb_convert_encoding() you can retrieve and store UTF-8 data even if
>> your app does not use it ;-)
>>
>>
>
> How's mb_convert_encoding() working? Is it correct in converting
> (assuming, of course, the character exists in the character set being
> converted to).

You can do something like this:

mb_convert_encoding(
htmlspecialchars($foo, ENT_COMPAT, 'UTF-8'),
'HTML-ENTITIES',
'UTF-8'
)

From "ABC <123> áéí € ㍝" you'd get:

ABC &lt;123&gt; &aacute;&eacute;&iacute; &euro; &#13149;

It's not something I'd recommend if you can avoid it but it can be done.


> I'm asking this because I only dabbled a bit in utf-8 several years ago,
> and haven't touched it since (yea, we Americans think the whole world
> revolves around us :) ). But I may soon be involved in a project which
> does use it.

Well, you Americans can switch to UTF-8 quite easily, given that's a
superset of good old 7-bit ASCII. It's not like in Europe, where failing
to recognise the file charset will ruin your data :)


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174533 is a reply to message #174508] Thu, 16 June 2011 21:34 Go to previous messageGo to next message
Jo Schulze is currently offline  Jo Schulze
Messages: 15
Registered: January 2011
Karma: 0
Junior Member
Luke wrote:

> I'm trying to migrate my project from ISO-8859-1 (Western Europe) to
> UTF-8. I *can* write special characters as regular text in my php-
> files, I can use special characters in echo-statements and I can even
> use special characters in .ini-files, load them, and display them. But
> when I load Strings with special characters from MySQL and display
> them, the string's "broken".

Which MySQL Server version are we talking about?

> <?xml version="1.0" encoding="utf-8" ?>
> and
> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

Both instruct the client that the data to be displayed should be UTF-8.
No verification done.

> in the markup. In the database I tried to set my table's collation to
> "utf8_general_ci".

As the name implies, the collation does only affect the sorting behavior
of MySQL. It does _not_ change the encoding of the database content.

> Also I tried the following functions:
>
> mysql_query("SET NAMES ‘utf8′");

good one

> mysql_set_charset('utf8');

good one (if avail)

> mb_internal_encoding("UTF-8");

Depends on your application and mbstring usage, usually bad choice.

> all not at the very beginning of my script, but before I queried the
> database and before I printed the results. I still keep getting
> 'wierd' characters.

1) Dump your DB to SQL
2) Convert the content from the former encoding to UTF-8 (eg. iconv)
3) Change MySQL metadata
4) Import the UTF-8 dump
5) Change DB wrapper code to negotiate UTF-8 as encoding (This is the
point where a DB wrapper comes in handy, if you haven't noticed before).
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174543 is a reply to message #174533] Fri, 17 June 2011 11:23 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 16/06/2011 23:34, Jo Schulze escribió/wrote:
>> mb_internal_encoding("UTF-8");
> Depends on your application and mbstring usage, usually bad choice.

Why exactly?

(I've found live systems where mb_internal_encoding defaulted to single
byte encoding, thus effectively running multi-byte data. So it's nice to
ensure it has a known value.)



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How do I force PHP to assume UTF-8 for $_GET?
Next Topic: radio button change after going to next page
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 15:00:15 GMT 2024

Total time taken to generate the page: 0.02316 seconds