user5365075 user5365075 - 2 years ago 108
PHP Question

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
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
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

Answer Source

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

$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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download