|
Posted by bob.savio on 09/24/06 21:26
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.republika.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.net/ob_iconv_handler> for usage information. This can encode
your output on the fly using iconv.
<?
// The ConvertCharsetOutputHandler 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.republika.pl/>. Include
// the class below:
require_once('ConvertCharset/ConvertCharset.class.php');
class ConvertCharsetOutputHandler
{
var $inputEncoding = null;
var $outputEncoding = null;
// How to use this class:
function test()
{
// Create the handler object
$handler =& new ConvertCharsetOutputHandler();
// Try to detect the Windows console encoding
// by calling chcp. Has no effect if we're not
// in CLI mode on Windows.
$handler->detectWinConsoleOutputEncoding();
// Acitvate the handler
$handler->activate();
// Get a character
$eAcute = html_entity_decode("é", ENT_QUOTES, 'ISO-8859-1');
// Print the character
echo "Acute accent character: $eAcute\n";
}
// Install the output handler and start buffering
function activate($inputEncoding = null, $outputEncoding = null)
{
if (!is_null($inputEncoding)) {
$this->inputEncoding = $inputEncoding;
}
if (is_null($this->inputEncoding)) {
$this->inputEncoding = 'ISO-8859-1';
}
if (!is_null($outputEncoding)) {
$this->outputEncoding = $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('ConvertCharsetOutputHandler: No input ' .
'encoding specified.', E_USER_ERROR);
$err = true;
}
if (!is_file(CONVERT_TABLES_DIR .
strtolower($this->inputEncoding))) {
trigger_error('ConvertCharsetOutputHandler: ' .
"Unknown input encoding \"{$this->inputEncoding}\".",
E_USER_ERROR);
$err = true;
}
if (is_null($this->outputEncoding)) {
trigger_error('ConvertCharsetOutputHandler: No output ' .
'encoding specified.', E_USER_ERROR);
$err = true;
}
if (!is_file(CONVERT_TABLES_DIR .
strtolower($this->outputEncoding))) {
trigger_error('ConvertCharsetOutputHandler: ' .
"Unknown output encoding \"{$this->outputEncoding}\".",
E_USER_ERROR);
$err = true;
}
if (!$err) {
$newBuf = $this->encoder->Convert($buf, $this->inputEncoding,
$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 detectWinConsoleOutputEncoding()
{
if (php_sapi_name() == 'cli' && isset($_SERVER['OS']) &&
preg_match('/win/i', $_SERVER['OS'])) {
$this->outputEncoding = $this->getDefaultWinCmdCodePage();
}
}
// Get the Windows code page using chcp
function getDefaultWinCmdCodePage()
{
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;
}
}
ConvertCharsetOutputHandler::test();
?>
- Bob S.
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.
[Back to original message]
|