wordpress 中的 PHP CSV 导出用 html 代码替换特殊字符

问题描述 投票:0回答:0

我正在尝试使用 PHP 将我的自定义帖子类型从 wordpress 导出到 CSV 中。

我成功地生成了我的 CSV 文件,没有任何问题,它将特殊字符(例如单引号

'
或破折号
-
)替换为 html 代码(例如
’

我试图从 php 文档中添加这一行 https://www.php.net/manual/fr/function.fputcsv.php 但它没有改变任何东西:

fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

我也尝试在我所有的琴弦上使用

utf8_encode
,但它没有用

在我的数据库中,字符串是正确的(意思是没有

’
)我什至尝试过
print_r()
我的数据,一切都很好,我觉得问题发生在 fputcsv 行,这是简化的代码:

 $header_args = array(
        'ID',
        'Titre',
        'Organisateurs',
        'Collaboration',
        'Région',
        'Public',
        'Règles du jeu',
        'URL Video',
        'Champs d\'investigation',
        'À propos',
        'Projet',
        'Contact',
        'Site de l\'organisateur'
    );
    
    $data = array($my_wp_query); // assume this is correct
    // print_r($data) shows everything as it should be
    

    $today = date("d-m-Y");
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=evenements-'.$today.'.csv');

    $output = fopen( 'php://output', 'w' );
    ob_end_clean();

    fputcsv($output, $header_args);

    foreach($data as $data_item){
        fputcsv($output, $data_item , ',' , "\"" , "\\");
    }
    fclose($output);
    exit;

举个例子,我的一个标题是

A l'ombre d'un nuage
在我的 CSV 文件中它输出“
A l’ombre d’un nuage"

php wordpress csv
© www.soinside.com 2019 - 2024. All rights reserved.