Thanks for your answer Colin. Based on Colin's suggestion, here are a
few options you can try that work for me:
- If you're in the Windows console, you could try changing the console
encoding to code page 1252 which is closer to ISO-8859-1. Run chcp to
do this:
C:\chcp 1252
You might need to change the font as well for this to work:
Properties -Font -Lucida Console
- You can also have PHP encode your output on the fly so that it
matches whatever the console is currently using. Below is an output
buffer handler class that uses the ConvertCharset tool by Mikolaj
(<http://mikolajj.republ ika.pl/>) and PHP's ob_start function. See the
method named "test" for usage information.
- You can also try the iconv module. See
<http::php.ne t/ob_iconv_handle rfor usage information. This can encode
your output on the fly using iconv.
<?
// The ConvertCharsetO utputHandler class encodes PHP output
// on the fly using the ConvertCharset class. See the "test" method
// for a usage example.
// Use the ConvertCharset class by Mikolaj which can be
// downloaded at <http://mikolajj.republ ika.pl/>. Include
// the class below:
require_once('C onvertCharset/ConvertCharset. class.php');
class ConvertCharsetO utputHandler
{
var $inputEncoding = null;
var $outputEncoding = null;
// How to use this class:
function test()
{
// Create the handler object
$handler =& new ConvertCharsetO utputHandler();
// Try to detect the Windows console encoding
// by calling chcp. Has no effect if we're not
// in CLI mode on Windows.
$handler->detectWinConso leOutputEncodin g();
// Acitvate the handler
$handler->activate();
// Get a character
$eAcute = html_entity_dec ode("é", ENT_QUOTES, 'ISO-8859-1');
// Print the character
echo "Acute accent character: $eAcute\n";
}
// Install the output handler and start buffering
function activate($input Encoding = null, $outputEncoding = null)
{
if (!is_null($inpu tEncoding)) {
$this->inputEncodin g = $inputEncoding;
}
if (is_null($this->inputEncoding) ) {
$this->inputEncodin g = 'ISO-8859-1';
}
if (!is_null($outp utEncoding)) {
$this->outputEncodi ng = $outputEncoding ;
}
if (!is_null($this->outputEncoding )) {
$this->encoder = new ConvertCharset;
ob_start(array( &$this, 'outputHandler' ));
}
}
// The output handler is called whenever output is produced
function outputHandler($ buf)
{
$err = false;
if (is_null($this->inputEncoding) ) {
trigger_error(' ConvertCharsetO utputHandler: No input ' .
'encoding specified.', E_USER_ERROR);
$err = true;
}
if (!is_file(CONVE RT_TABLES_DIR .
strtolower($thi s->inputEncoding) )) {
trigger_error(' ConvertCharsetO utputHandler: ' .
"Unknown input encoding \"{$this->inputEncoding} \".",
E_USER_ERROR);
$err = true;
}
if (is_null($this->outputEncoding )) {
trigger_error(' ConvertCharsetO utputHandler: No output ' .
'encoding specified.', E_USER_ERROR);
$err = true;
}
if (!is_file(CONVE RT_TABLES_DIR .
strtolower($thi s->outputEncoding ))) {
trigger_error(' ConvertCharsetO utputHandler: ' .
"Unknown output encoding \"{$this->outputEncoding }\".",
E_USER_ERROR);
$err = true;
}
if (!$err) {
$newBuf = $this->encoder->Convert($buf , $this->inputEncodin g,
$this->outputEncoding );
return $newBuf;
}
return $buf;
}
// Try to detect the Windows console character encoding using
// chcp. This has no effect if we're not in CLI mode on Windows.
function detectWinConsol eOutputEncoding ()
{
if (php_sapi_name( ) == 'cli' && isset($_SERVER['OS']) &&
preg_match('/win/i', $_SERVER['OS'])) {
$this->outputEncodi ng = $this->getDefaultWinC mdCodePage();
}
}
// Get the Windows code page using chcp
function getDefaultWinCm dCodePage()
{
exec('chcp 2>&1', $output, $exitCode);
$outputStr = join("\n", $output);
if ($exitCode != 0) {
trigger_error(" Failed to execute chcp: $outputStr",
E_USER_ERROR);
} else {
if (preg_match('/^Active code page: (.*)$/', $outputStr,
$matches)) {
return 'CP' . $matches[1];
} else {
trigger_error(" Failed to read chpc output: $outputStr",
E_USER_ERROR);
}
}
return null;
}
}
ConvertCharsetO utputHandler::t est();
?>
Colin McKinnon wrote:
yellowtek wrote:
Hi,
I'm simply using PHP as a programming language, and I just want to
print some text information to stdout,
but instruction
print "é" (é)
does not print my "e" with an accent in the shell window: wrong
charset.
How to set the correct charset?
This has always been one of PHP's weak spots, and something I've never had
to fix fortunately.
AFAIK, you need to find out what encoding and charset your console is using
- then use binary strings.
HTH
C.