Character encoding issues with PHP generated vCard

2019-06-27 08:22发布

问题:

We're encountering character encoding issues trying to create vcards in PHP.

In Outlook names that use special characters are distorted, like "é" becomes "é".

We updated the header and the FN and N sections for Windows character encoding, but the issue remains.

Grateful for any suggestions.

Vcard excerpt:

BEGIN:VCARD
VERSION:3.0
REV:2013-03-27 19:37:46
FN;CHARSET=Windows-1252:Namé S. Nameé
N;CHARSET=Windows-1252:Namé;Namé;;;
TITLE:Associate
ORG:Company
EMAIL;TYPE=internet,pref:name@abc.com
TZ:-0400
END:VCARD

PHP Header for Vcard:

    header("Content-type: text/x-vcard; charset=windows-1252;");
    header("Content-Length: ".strlen($vc->card));
    header("Content-Disposition: attachment; filename=".$vcard_name.".vcf");
    header("Pragma: public");

回答1:

There were some similar questions, but nothing seemed definitive on this. By specifying the charset in the vcard fields it looks like I was half way there.

I finally got it working by changing the following to "utf-8;":

 header("Content-type: text/x-vcard; charset=CHARSET=utf-8;");

The same goes for the name fields in the vcard itself. Specifying utf-8 seems to have resolved the display issues of the special characters:

$this->card .= "FN;CHARSET=utf-8:".$new_display_name.$this->data['short_mode'];
    $this->card .= "N;CHARSET=utf-8:"

Opened in Outlook 2007 with all the accent characters displaying as intended.



回答2:

Your solution didn't work for me, I still got funny characters on Windows.

What worked for me was using ISO-8859-1 instead. You can convert UTF8 to ISO-8859-1 in PHP using utf8-decode() , and by using ENCODING=iso-8859-1 in the Vcard for the relevant fields makes it work on most UTF-8 based clients I have tested on.

Header:

Content-Type: text/x-vcard; charset=iso-8859-1

Vcard example:

N;CHARSET=iso-8859-1:Göteborg

Tested on Windows, OS X, IOS and Android.