473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

PHP standalone: charset setting

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?

Thanks

Herve.

Sep 22 '06 #1
3 4246
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.
Sep 24 '06 #2
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_handlerfor 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("&eacute;", 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();

?>
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 "é" (&eacute;)
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.
Sep 24 '06 #3
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_handlerfor 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("&eacute;", 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 "é" (&eacute;)
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.
Sep 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ricardo Garcia | last post by:
hi, i put as you said encoding="UTF-8" (<?xml version="1.0" encoding="UTF-8" standalone="no"?> as first line), but when i tried to validate in http://validator.w3.org, i get the following response:...
23
by: Brian | last post by:
Despite charset being discussed to death, and despite having followed all of those threads, I still have problems. http://www.tsmchughs.com/recipes/soda-bread This page uses iso-8859-1...
4
by: Umesh | last post by:
Hi all I am trying to change the printer(like paper source ,etc) setting in C#. But i am unable to do it. I am not sure how to do it. should i have to use WIN API like Openprinter , getprinter ,...
35
by: The Bicycling Guitarist | last post by:
My web site has not been spidered by Googlebot since April 2003. The site in question is at www.TheBicyclingGuitarist.net/ I received much help from this NG and the stylesheets NG when updating the...
1
by: Dean Slindee | last post by:
Currently using: Dim xtw As New XmlTextWriter (path,system.Text.Encoding.UTF8) which produces: <?xml version="1.0" encoding="utf-8" standalone="yes" ?> how can I produce: <?xml...
4
by: Rémi | last post by:
Question: How can you determine the character set used by a webpage you built? My understanding of the issue is that the character set used by an HTML file (or any other file, for that matter)...
7
by: gmclee | last post by:
Hi there, I am writing a program to load HTML from file and send it to IE directly. I've met some problem in charset setting. Most of HTML have charset "us-ascii", for some reason, some UNICODE...
2
by: vunet.us | last post by:
I translate my website to German and some ASCII characters are not translated by my browser. For example I see this: &#x00FC; I use charset: <meta http-equiv="Content-Type" content="text/html;...
0
VietPP
by: VietPP | last post by:
Hi all, I've asked too much question in this day, hehe. I'm trying to export my table data in OracleDB to excel. The problem is my charset in database is US7ACSII (using Vietnamese font), when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.