UTF-8, PHP and Mysql XML

I am having big problems solving this problem:

I have a mysql latin1_swedish_ci database encoding and a table that stores names and addresses.

I am trying to output a UTF-8 XML file, but I am having problems with the following line:

OtivägenIt is displayed as Otivägenwhen I vim the file. Also when opening IE I get

" An invalid character was found in text content. Error processing resource"

I have the following code:

function fixEncoding($in_str)
{
    $cur_encoding = mb_detect_encoding($in_str) ;
    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
        return $in_str;
    else
        return utf8_encode($in_str);
}

header("Content-type: text/plain;charset=utf-8");
$mystring = "Otivägen" // this is actually obtained from database;

$myxml = "<myxml>
....
     <node>".$mystring."</node>
....
</myxml>
";
$myxml = fixEncoding($myxml);

Actual XML output is below:

<?xml version="1.0" encoding="UTF-8" ?>
<myxml>
    ....
    <node>Otivägen</node>
    ....
</myxml>

Any ideas how I can output the file, so in the vim the file reads Otivägen, not Otivägen?

EDIT:

I did mysql_client_encoding()and got latin1
I then did mysql_set_charset()
and ran again mysql_client_encoding()and got utf8, but still the same output problems.

Edit 2

I went to the command line and ran a query SELECT address1 FROM address WHERE id = 1000;

SELECT address1 FROM address WHERE id = 1000;
Current database: ftpuser_db

+-------------+
|   address1  |
+-------------+
| Otivägen 32 |
+-------------+
1 row in set (0.06 sec)

Thanks in advance!

+3
6

, , , .

UTF-8 ä - C3 A4, ä, Latin-1.

+1
+2

, . UTF8 , , - .

UTF8. , "" . , , - (, ).

UTF8 , UTF8 , , . . .

:

  • , UTF8. -. . ETerm .
  • LANG . , .UTF-8 .
  • , vim UTF8. :set encoding

, UTF8.

MySQL.

MySQL CLI show variables like 'character_set%';. , , :

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     | 
| character_set_connection | latin1                     | 
| character_set_database   | latin1                     | 
| character_set_filesystem | binary                     | 
| character_set_results    | latin1                     | 
| character_set_server     | latin1                     | 
| character_set_system     | utf8                       | 
| character_sets_dir       | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+

, , - latin1 ( , ) utf8.

set names utf8; , . , . my.cnf, . .

, , .

, "".

+2

latin1_swedish_ci - , . , , latin1, .

, , MySql /. , (mysql_set_charset). . . - , ascii. ?

$mystring = "Otivägen" // this is actually obtained from database;

. $mystring php. , .

0

SET NAMES utf8

SET NAMES latin1

,

0

, " " Otivägen. , Otivägen UTF-8, utf8_encode(). :

$str = "Otivägen"; // already an UTF-8 string
echo utf8_encode($str); // outputs Otivägen

, " ", . . , Aptana Studio: ISO-8859-1 ( Aptana , "". , Aptana → → ). , PHP , $myxml <myxml><node>..., ISO-8859-1, $mystring, , UTF-8. fixEncoding else, $myxml ISO-8859-1, UTF-8. .

, UTF-8. , fixEncoding/utf8_encode/utf8_decode $myxml. , Otivägen.

0

Source: https://habr.com/ru/post/1723793/


All Articles