重音字符出现在文本文件中,但不出现在终端(osx)中


Accented characters appear in text file but not in terminal (osx)

我一直在从数据库中检索数据,我一直有麻烦得到重音字符显示(它们出现为?,我已经尝试了很多事情试图让这个工作)。

我只是尝试将数据放入使用file_put_contents的文件中,这意味着问题可能是终端本身,而不是字符编码,除非我错了?然后我尝试用file_get_contents读取具有正确重音字符的文件,它仍然在终端

中显示为?

有没有人知道我如何才能在终端中显示包含重音字符的数据?如果我尝试回显一个简单的é,它显示完美。

谢谢!

我代码:

<?php
ini_set('default_charset','utf-8');
header('Content-Type: text/html; charset=utf-8');
$username = "username";
$password = "password?";
$dsn = "dsn";
$connection = odbc_connect($dsn, $username, $password, SQL_CUR_USE_ODBC);
$sql = "SELECT '"Insert ID'", '"Date Created'", '"Date Modified'", Description FROM Insert";
$res = odbc_exec($connection,$sql);
$x=0;
while ($row = odbc_fetch_array($res)){
$x++;

   $values= ($x . ": " . " Insert ID:". $row['Insert ID'] . " Date Created: " . $row['Date Created'] . " Date Modified:" . $row['Date Modified'] . " Dscription:" . $row['Description'] . "'n");


   print($values);
}
odbc_free_result($res);
odbc_close($connection);

您的PHP文件很可能保存为ISO Latin 1或Mac OS Roman。将其更改为UTF-8应该可以工作。我刚刚在我的Mac上试过,它输出'特殊'字符没有问题:

<?php
//test.php
$data = 'é';
file_put_contents('./test.txt', $data);
$output = file_get_contents('./test.txt');
echo $output.PHP_EOL;

在Terminal.app中运行脚本:

 $ php test.php //outputs 'é'