Alasdair
Alasdair

Reputation: 14161

UTF8 -> Latin1 Difficulty, PHP

I'm losing accented characters.

From PHP I download an xml file which uses UTF8, while my PHP script uses Latin1. I can't manage to convert the UTF8 into Latin1.

I've tried this:

$meta=mb_convert_encoding($meta,'CP1252','UTF-8');

and

$meta=mb_convert_encoding($meta,'UTF-8');
$meta=mb_convert_encoding($meta,'CP1252','UTF-8');

But either way the accented characters are broken and turned into 2 characters.

Input:

<title>First book of zoölogy</title>

Output:

<title>First book of zoo?logy</title>

I figured it out myself, see my answer below. Thank you everyone for your help!

Upvotes: 0

Views: 1476

Answers (3)

Alasdair
Alasdair

Reputation: 14161

This fixed it:

$meta=iconv('UTF-8','CP1252//TRANSLIT',$meta);

I didn't know about iconv before, I thought there was only mb_strings to work with, but iconv works very well.

Upvotes: 1

Valeh Hajiyev
Valeh Hajiyev

Reputation: 3246

Maybe default charset of your MySQL server is UTF-8. Try this:
Insert the following query after your MySQL connection details:

mysql_query("SET NAMES latin1");

Upvotes: 0

Change the collation of the tables do Utf8_general_ci and before conections to the database use:

mysql_set_charset("utf8");

I think this can solve your problem.

Upvotes: 1

Related Questions