这是我的php代码,它应该写一个CSV文件,但输出文件包含无法阅读的字母和符号.我应该怎么做才能让信息用波斯字母书写?

<?php
// Get RSS feed URL from form data
$rss_url = $_POST['rss-url'];

// Create SimpleXMLElement object from RSS feed
$rss = new SimpleXMLElement(file_get_contents($rss_url));

// Open CSV file for writing
$filename = 'rss-data.csv';
$file = fopen($filename, 'w');

// Write header row to CSV file
$header_row = array('Title', 'Description', 'PublicationDate', 'Link', 'Photo');
fputcsv($file, $header_row);

// Loop through RSS feed items
foreach ($rss->channel->item as $item) {
    // Extract data from item
    $title = mb_convert_encoding((string) $item->title, 'UTF-8', 'ISO-8859-1');
    $description = mb_convert_encoding((string) $item->description, 'UTF-8', 'ISO-8859-1');
    $pub_date = mb_convert_encoding((string) $item->pubDate, 'UTF-8', 'ISO-8859-1');
    $link = mb_convert_encoding((string) $item->link, 'UTF-8', 'ISO-8859-1');
    preg_match('/<img.*?src="(.*?)".*?>/is', $description, $matches);
    $image = isset($matches[1]) ? $matches[1] : '';

    // Write data to CSV file
    $data_row = array($title, strip_tags($description), $pub_date, $link, $image);
    fputcsv($file, $data_row);
}

// Close CSV file
fclose($file);

// Redirect to index.html
header('Location: index.html');
exit();
?>

我的php代码应该会在其中编写一个包含波斯字母的CSV文件.

推荐答案

您应该在开始时使用以下代码.

header('Content-Type: text/html; charset=UTF-8');
setlocale(LC_ALL, 'fa_IR.utf8');

以下是修改后的完整代码.

<?php
// Set the character encoding to UTF-8
header('Content-Type: text/html; charset=UTF-8');
setlocale(LC_ALL, 'fa_IR.utf8');

// Get RSS feed URL from form data
$rss_url = $_POST['rss-url'];

// Create SimpleXMLElement object from RSS feed
$rss = new SimpleXMLElement(file_get_contents($rss_url));

// Open CSV file for writing
$filename = 'rss-data.csv';
$file = fopen($filename, 'w');

// Set the encoding of the CSV file to UTF-8
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));

// Write header row to CSV file
$header_row = array('Title', 'Description', 'PublicationDate', 'Link', 'Photo');
fputcsv($file, $header_row);

// Loop through RSS feed items
foreach ($rss->channel->item as $item) {
    // Extract data from item
    $title = htmlspecialchars_decode((string) $item->title, ENT_QUOTES | ENT_XML1);
    $description = htmlspecialchars_decode((string) $item->description, ENT_QUOTES | ENT_XML1);
    $pub_date = (string) $item->pubDate;
    $link = (string) $item->link;
    preg_match('/<img.*?src="(.*?)".*?>/is', $description, $matches);
    $image = isset($matches[1]) ? $matches[1] : '';

    // Write data to CSV file
    $data_row = array($title, $description, $pub_date, $link, $image);
    fputcsv($file, $data_row);
}

// Close CSV file
fclose($file);

// Redirect to index.html
header('Location: index.html');
exit();
?>

Php相关问答推荐

有条件的运费率基于购物车小计与WooPayments货币转换

累加平面数组中数字子集的所有组合,以生成组合及其乘积的二维数组

WordPress:将自定义单选输入字段添加到用户配置文件

WooCommerce产品按特定元数据的自定义数字排序选项

在WooCommerce中禁用客户用户角色的纳税计算

从目录文件夹中获取文件并追加到 Select 下拉列表(选项)-wordpress/wc

返回WooCommerce中的所有已启用变体

移动应用:在PHP中忽略Cookie?

文件::Files()响应返回空对象数组

根据类别的数量折扣更改WooCommerce购物车商品价格

将数据推入所有子数组条目

未定义的类型';PDF';在Laravel 8

除WooCommerce中的特定国家/地区外,有条件地要求填写邮政编码 checkout 字段

如何在PHP中根据会话将用户重定向到不同的页面?

当未 Select 任何变体时,在 WooCommerce 可变产品上显示自定义文本

主机中未检索用户表数据

使用产品自定义字段作为 WooCommerce 中特定用户角色的折扣百分比

如何在PHP中对多个脚本调用进行排队?

保留 .htaccess 中的符号登录 URL

php preg_match 验证 Tiktok 用户名