473,804 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4269
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.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("&eacute;", 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 "é" (&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.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("&eacute;", 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();

?>

- 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
2006
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: The HTTP Content-Type header sent by your web browser (unknown) did not contain a "charset" parameter, but the Content-Type was one of the XML text/* sub-types (text/xml). The relevant specification (RFC 3023) specifies a strong default of...
23
3956
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 charset. From the headers: Content-Type: text/html; charset=iso-8859-1 Under ingredients are characters for fractions (188, 189, and 190) and the degree character (176), characters which Lynx can display on
4
4748
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 , setprinter . I did try with PrinterSetting.setHDevMode but it does not work. Its their is a simple way to change printersetting. thanks umesh.
35
4648
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 code before then. My host's tech guy just sent me the following. Isn't it okay to specify UTF-8 as the charset in the HTTP headers at the server level? Isn't it okay to have validated XHTML 1.0 strict code? ...
1
2557
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 version="1.0" encoding="UTF-8" standalone="no" ?>
4
2069
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) depends on your own system, and the encoding used by it; you cannot randomly insert a <META HTTP-EQUIV="Content-Type" content="text/html; charset=xxx-1234-567">
7
5982
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 TEXT will be inserted into the HTML before sending to IE. The problem is 1) Can I specify special charset for some component, e.g. <span charset="UTF-8"SOME UNICODE HERE</spand> 2) If "NO" for 1), so any way to change the charset of the...
2
3809
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; charset=iso-8859-1"> How can I determine correct charset? Thanks
0
1585
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 I exprort to excel it's changed to Unicode UTF-8 so all of the characters had lost, text cannot be display correctly. Is it possible to add a charset setting to code. Thank you!
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.