简体   繁体   中英

PHP encoding problems

After a webhook (XML) trigger, I have a PHP code doing the following treatment:

$xmlData = fopen('php://input' , 'rb'); 
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);

file_put_contents('orders/order' . date('m-d-y') . '-' . time() . '.xml', $xmlString, FILE_APPEND);

And I also transfer this info to a database:

$xml = new SimpleXMLElement($xmlString);
$address1 = trim($xml->{'billing-address'}->address1);
$sql="INSERT INTO `Customers`(`address1`)
VALUES
('$address1')";

My problem is that character is not properly transported for the xml file and the database.

Original statement: São Paulo

XML file saved on the server:

<?xml version="1.0" encoding="UTF-8"?>
 <address1>S&#xE3;o Paulo</address1>

Information on database (utf8_general_ci):

São Paulo

Everything seems to be proper set to UTF-8 but I still have this character problems.

When you establish database connection run the query:

SET NAMES 'utf8

That should help. Also make sure the data coming from your "web hook" is in that format.

I think the php functions you are using are not intended to manage multibytes string. Try putting

mb_internal_encoding("UTF-8");

at the beginning of your php code and have a look to the Multibyte String Functions page on php.net to understand if you need to change some function in favour of its corresponding multibyte version.

As a last chance you might try to iconv the string before inserting it to the database:

$address1 = trim($xml->{'billing-address'}->address1);
$address1 = iconv(iconv_get_encoding($string), "UTF-8", $address1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM