wordpress 德语文本在Outlook桌面版中显示不正确(vCard)

eqqqjvef  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(153)

我有一个PHP函数,它可以从WordPress ACF插件生成vCard数据。德语文本在Outlook的桌面版本中无法正确显示,但在移动的版本上可以正常工作。下面是我用来生成vCard的代码:

function generate_vcard($post_id) {
    // Get ACF field values for the post
    $title = utf8_encode(get_field('title', $post_id));
    $first_name = utf8_encode(get_field('first_name', $post_id));
    $last_name = utf8_encode(get_field('last_name', $post_id));
    $position = utf8_encode(get_field('position', $post_id));
    $company = utf8_encode(get_field('company', $post_id));
    $birthday = utf8_encode(get_field('birthday', $post_id));
    $office_phone = utf8_encode(get_field('office_phone', $post_id));
    $cell_phone = utf8_encode(get_field('cell_phone', $post_id));
    $email_1 = utf8_encode(get_field('email_1', $post_id));
    $email_2 = utf8_encode(get_field('email_2', $post_id));
    $website = utf8_encode(get_field('website', $post_id));

    // Get profile image URL from ACF field
    $profile_image = get_field('profile_image', $post_id);

    // Encode the image to base64
    $image_data = file_get_contents($profile_image);
    $base64_image = base64_encode($image_data);

    // Generate vCard content
    $vcard = "BEGIN:VCARD\r\n";
    $vcard .= "VERSION:3.0\r\n";
    $vcard .= "PHOTO;TYPE=JPEG;ENCODING=b;VALUE=URL:$base64_image\r\n";
    $vcard .= "FN:$first_name $last_name\r\n";
    $vcard .= "N:$last_name;$first_name;;;\r\n";
    $vcard .= "ORG;CHARSET=windows-1252:$company\r\n";
    $vcard .= "TITLE:$position\r\n";
    $vcard .= "BDAY:$birthday\r\n";
    $vcard .= "TEL;TYPE=WORK,VOICE:$office_phone\r\n";
    $vcard .= "TEL;TYPE=CELL,VOICE:$cell_phone\r\n";
    $vcard .= "EMAIL:$email_1\r\n";
    $vcard .= "EMAIL:$email_2\r\n";
    $vcard .= "URL:$website\r\n";
    $vcard .= "END:VCARD\r\n";

    // Set headers to trigger download with Windows-1252 encoding
    header("Content-Type: text/vcard; charset=windows-1252");
    header("Content-Disposition: attachment; filename=contact.vcf; charset=windows-1252");

    // Output vCard content with Windows-1252 encoding
    echo $vcard;
    exit;
}

add_action('wp', 'export_vcard');
function export_vcard() {
    if (isset($_GET['export_vcard']) && $_GET['export_vcard'] == 1) {
        $post_id = get_the_ID();
        generate_vcard($post_id);
    }
}

字符串
我的意思是vCard中的德语文本在Outlook的桌面版本中未按预期呈现。德语文本显示为für,但实际文本为für
x1c 0d1x的数据
我已经尝试将vCard文件的编码更改为UTF-8,但这并没有解决问题。有人知道为什么会发生这种情况以及如何修复它吗?

weylhg0b

weylhg0b1#

WordPress默认使用utf8mb4,所以你不能get_field上使用utf8_encode,因为这会导致双重编码。(vCard 3.0的推荐默认值,vCard 4.0的必须值)创建vCard和设置标题时。不要添加BOM。所有小写都使用utf-8,由于某些版本的Outlook不接受所有大写字母。
创建一个最小可重复的示例,然后从那里开始。我已经测试过:

<?php

// Sample data
$first_name = 'Björn';
$last_name = 'Ängstler';
$company = 'Arzt für innere Medizin';

// Create vCard
$vcard = "BEGIN:VCARD\r\n";
$vcard .= "VERSION:3.0\r\n";
$vcard .= "FN;CHARSET=utf-8:$first_name $last_name\r\n";
$vcard .= "N;CHARSET=utf-8:$last_name;$first_name;;;\r\n";
$vcard .= "ORG;CHARSET=utf-8:$company\r\n";
$vcard .= "END:VCARD\r\n";

// Set headers to trigger download
header("Content-Type: text/vcard; charset=utf-8");
header("Content-Disposition: attachment; filename=contact.vcf; charset=utf-8");

// Output vCard content
die($vcard);

字符串
如果你仍然遇到问题,从有问题的客户端(例如Outlook)导出一个vCard,然后在文本编辑器中打开导出和你的vCard进行比较。

相关问题