Exn
Exn

Reputation: 809

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

I have been working on retrieving data from a database, I have been having trouble getting the accented characters to show (they appear as ? and I have tried many things to try and get this to work).

I just tried putting the data into a file using file_put_contents which means the issue might be with the terminal itself and not with the character encoding, unless I am wrong? I then tried file_get_contents to read the file with the correct accented characters, and it still shows as ? in the terminal

Does anyone have any idea how I could get to show the data in the terminal with accented characters included? If I try to echo a simple é it shows up perfectly.

Thanks!

My code:

<?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);

Upvotes: 0

Views: 897

Answers (1)

cOle2
cOle2

Reputation: 4784

Your PHP file is most likely saved as ISO Latin 1 or Mac OS Roman. Change it to UTF-8 and it should work. I just tried it on my Mac and it output 'special' characters no problem:

<?php
//test.php
$data = 'é';

file_put_contents('./test.txt', $data);

$output = file_get_contents('./test.txt');

echo $output.PHP_EOL;

Run the script in Terminal.app:

 $ php test.php //outputs 'é'

Upvotes: 1

Related Questions