PHP 生成的 DBF 文件中存在特殊字符 (ñ) 的问题

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

描述:

我在使用 PHP 的 dbase 扩展生成 DBF 文件时遇到困难。当数据中包含“ñ”等特殊字符时就会出现问题。

问题:

生成的 DBF 文件包含特殊字符的 ASCII 值,而不是实际字符。 示例:“Jane ñ Doe”在 DBF 文件中变为“Jane ├▒ Doe”。

我尝试使用 header('Content-Type: text/html; charset=utf-8'); 设置 PHP 中的字符编码,但问题仍然存在。 我尝试过将特殊字符转换为其他编码,但结果并不符合预期。

header('Content-Type: text/html; charset=utf-8');

// Set the DBF file path
$dbfFilePath = 'D:\INCOME\new.dbf';

// Create a DBF file
$dbf = dbase_create($dbfFilePath, [
    ['ID', 'N', 5, 0],
    ['Name', 'C', 50],
]);

// Add sample data with special characters
foreach ([[1, 'John Doe'], [2, 'Jane ñ Doe'], [3, 'Alice']] as $row) {
    dbase_add_record($dbf, $row);
}

// Close the DBF file
dbase_close($dbf);
php character-encoding special-characters dbase
1个回答
0
投票

我认为你应该在 PHP 中使用

iconv()
mb_convert_encoding()
将字符串编码从 UTF-8 转换为所需的编码。例如,如果您发现 Windows-1252 可以工作,您可以使用类似以下内容 -->

$encodedName = iconv('UTF-8', 'Windows-1252', 'Jane ñ Doe');

mb_convert_encoding

$encodedName = mb_convert_encoding('Jane ñ Doe', 'Windows-1252', 'UTF-8');

因此在上面的示例中

'Jane ñ Doe'
(UTF-8 格式)被转换为 Windows-1252 编码。然后在数据库记录创建中使用
$encodedName

dbase_add_record($dbf, [2, $encodedName]);
© www.soinside.com 2019 - 2024. All rights reserved.