开发者

PHP generated Vcard: Change character encoding so accented character can be displayed

I'm using a PHP class to generate vcard: http://www.troywolf.com/articles/php/class_vcard/

But accented character do not show properly. I guess this is related to the character encoding, but I have been unable to find a way to change it.

All my files are saved as UTF8 without BOM and here is what the outpout of the script looks like:

BEGIN:VCARD
VERSION:3.0
CLASS:PUBLIC
PRODID:-//www.bla.com//Version 1//EN
REV:2011-08-30 03:19:49
FN:Heïdi DABODFE
N:DABODFE;Heïdi;;;
TITLE:Directrice
ORG:blablabla
ADR;TYPE=work:;;2 Imp. Les Plataness;Mirepoix;;09500;France
EMAIL;TYPE=internet,pref:contact@bla.com
TEL;TYPE=work,voice:05 61 68 64 38
PHOTO;ENCODING=BASE64;TYPE=JPEG:
/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkc
gSlBFRyB2NjIpLCBxdWFsaXR5ID0gOTAK/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCA
wKDAwLCgsLDQ4SEA0OEQ4LCxAWEBETFBUVFQwPFxgWFBgSFBUU/9sAQwEDBAQFBAUJBQUJFA0LD
RQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgA
YABgAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwM
CBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJS
YnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVl
peYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX2
9/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQ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T2Fpc6Vq8D6psSEFJmk
mfeksYOPkK53dTyM4x7Vk6xcxvrsNjPI0Wn2hW3BUZ2gfefHcsSWP1+la2q3dhpGlW1ha38OryJ
eNdK0Qfy4o8ABcsFOWwCQBgbRW576k3bzPZfBXx51LwBdJaS2Md3p2547uXzw8E67jjCYwuBxt9
gMdq+l/h98VNI8TWsc/hrVol3fe0u9kwAfRH5I+hB/AV+exuLSOJLjzFku2mD+XGCFCY+bdkYBJ
xgD39q3NLe8luje25fT0MZEM0c6wbmC+jHL5Ixgf0rCdJPVaHg47I6GJXPB8kvwfyP0yi8cRWwA
1OzuLBum90zGf+BjK/rWhB4w0e5UGPUIXB/utn+Vfn94R/aL8Z+FElt7nVGMsJCiC5XB9+owPy/
nXYw/teak2mJd3uh2Fy7StEd0SueFBySQOuf0rF05o+WqZBjqbtFKS8n/AJ2Ps248ZaNajMmoQr
7M2P51Qn8atdIf7MsJ7kf89pV8uIf8CbAP4Z+lfJ0f7WczaYbuBdN02VVfNvFbkT7h90LhSpB4y
SRivNNY/aD8U+MZGTU9ZNpaE5kSHcGZcHheCM/lQqc2OjkONqv3lypdz6n+I/xk0LwlC82t6lFr
F6oJj021bMII/vd3x6YA45Hevlb4kfFXUvi7JKXuQAQBDYlxHtO4YxnAPAICgknI7jNefLf3etw
mERtdXO4M8znLBcnJLE4A5A5qC9sP7K1C2mXKWjFSs3mLKqtgErvXgkH/APVW8KSjq9z67AZPQw
L5pPmn3/4BXPnalLIqyRq1rCxRWyCVTLHHGMgZPOOnrVmLTtSfTo/It7ezmnjCq8t2kUsyZBG1G
YHkgcgc4475g04xLrscKTNPZlmkePccSqoLbT9do/Os+zeHxDqly+qaitpLMjOlxOG2GTsG2gkD
8K3PbnJv0P/Z

URL;TYPE=work:www.bla.com
NOTE:bla
TZ:-0500
END:VCARD

Any help is much appreciated


Did you try some different programs to import this VCARD? For Version 3.0 they should be fine with utf-8 unless its name starts with "O" and ends with "tlook" ^^

See if this works for you:

FN;CHARSET=utf-8:Heïdi DABODFE
N;CHARSET=utf-8:DABODFE;Heïdi;;;


It would be good if programs would really follow the standards! This is how they should work:

  • For vCard 2.1, you should use FN;CHARSET=utf-8;ENCODING=8BIT:Heïdi DABODFE, because it could also be ENCODING=QUOTED-PRINTABLE and many programs really do use that. I think Outlook eats this well, however Thunderbird rejects the ENCODING part (Thunderbird also exports supposedly v2.1 vCards without charset/encoding information).
  • For vCard 3.0 the possibility of using a different charset for each property disappeared (what sense did it made?). You are to use the Content-type MIME header when transferring the vCard and that's where the charset is to be specified (says section 5 of the RFC). However, you don't usually find Content-type headers in vCard files and most programs just use FN;CHARSET=utf-8;ENCODING=8BIT:Heïdi DABODFE as it was for 2.1.
  • For vCard 4.0 the charset for vCard files is always UTF-8 and there is no way to override this and it is invalid to specify a different charset in a MIME header (section 3.1 of the RFC).

The reality is that most programs treat vCard with very little care and use the same code to handle versions 2.1, 3.0 and 4.*. I generate vCard using UTF-8 to satisfy RFC 6350, use CHARSET=utf-8 to satisfy vCard 2.1 specification, avoid ENCODING=8BIT to satisfy Thunderbird and I'm currently considering adding a MIME header.

It is unfortunate that in 2015 there is still difficulty in dealing with this problem which sounds more like a problem from the 1980s.


The solution is rather something like this,

assuming $vcf to be a string containing utf-8 characters:

file_put_contents($dir.$filename, utf8_decode($vcf));

French utf-8 characters get displayed properly in Outlook, just found out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜