In PHP code, use exec
to execute nodejs command
The Chinese characters received by nodejs are garbled characters
All operations are run in CLI mode (that is, run under the command line)
Executedchcp 65001
Switch code page, and PHP and NodeJS files are UTF8 encoded (no BOM header)
Run the NodeJS command directly from the command line, and the received value is normal
When running PHP on the command line, the Chinese characters received by NodeJS are garbled
Windows 10 (1703 OS Build 15063.296)
PHP 5.5.7 (cli)
Zend Engine v2.5.0
node v6.9.2
Command line: cmd V10.0.15063
Test script 1: test.php (php script)
Test script 2: test.js (nodejs script)
Other factors that may affect:
test.js uses the yargs
package to receive command line parameters
Because npm
was unable to connect for a long time, I used the Taobao source cnpm
command to replace the npm
command
<?php
print(exec('node test.js -t "红掌拨清波"'));
?>
'use strict';
const yargs = require('yargs');
var argv = yargs
.options({
't': {
alias: 'text',
demandOption: true,
default: '',
describe: 'Source text.',
type: 'string'
}
})
.argv;
console.log(argv.t);
Open cmd.exe
, enter chcp 65001
, and switch the encoding to UTF-8
.
Run test.js
first, and pass in the parameters -t "White hair and green water"
, normal
Running test.php
, outputting garbled characters
May I ask what caused this situation and how to deal with it? Thank you
Windows cmd defaults to gbk encoding, so the transmission of utf8 will be garbled