Pakeski
Pakeski

Reputation: 51

How to safelycompare UTF-8 to ISO 8859-1 (latin1) in PHP?

This might be a stupid question, but nothing seems to be working for me:

I'm having to compare values between 2 columns on 2 different databases (which I don't have access to change the values). The encoding in db1 is UTF-8. The encoding in db2 is latin1.

So, for example, these are the 2 values I'm comparing and should be the same in the comparison:

**db1_value** = 'Maranhão'
**db2_value** = 'Maranhão';

They display exactly the same way using utf_encode, displaying is not the issue. I'd like to compare the variable db1_value to the field db2_value in the db, so I'm using something very simple like this:

$query = "SELECT **db2_value** FROM db2 WHERE db2_field LIKE '" . **$db1_value** . "'";

How do I convert 'Maranhão' into '**Maranhão**' before comparing?

I've tried several methods, iconv, utf8_encode, and a few others, but they make no difference to the variable. I'm just wondering if I'm taking the right approach to do this.

Appreciate any constructive comments on this.

Thanks a lot,

Upvotes: 2

Views: 4374

Answers (1)

Dmitri
Dmitri

Reputation: 36280

You need to convert not from UTF-8 but from HTML-ENTITIES into actual value Luckily mbstring extension has such conversion available:

$latin1 = mb_convert_encoding($db1_value, "ISO-8859-1", "HTML-ENTITIES");

Here we specify the HTML-ENTITIES as the FROM charset

Then you can compare $latin1 to your $db2_value.

Upvotes: 5

Related Questions