简体   繁体   中英

PHP unexpected character encoding

I have a PHP script that fetches an ICS file, makes some changes to it, and rewrites it for download using Content-Type: application/ics and Content-Disposition: attachment; filename="test.ics" Content-Disposition: attachment; filename="test.ics" . The script works fine. However, some characters like ' get converted to ' , but not all of them, only those in lines that I have modified.

When I var_dump the string that is being written as payload, the browser displays all characters fine.

Any idea ?

EDIT: I use an ICS parser from here . The problem comes from it but after looking through the code I can't find where it could replace some characters by htmlentities .

You'll need to converts &#[0-9]+ entities to UTF-8 , you can use preg_replace_callback and mb_convert_encoding for that, ie:

$string = "stack user's";
echo preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $string); 
//stack user's

Ideone Demo

I think you need to decode html characters while you are modifying the file:
http://php.net/manual/en/function.htmlspecialchars-decode.php

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