node.js - When PHP uses exec to execute nodejs, the Chinese characters passed into JS are garbled
怪我咯
怪我咯 2017-06-30 09:53:51
0
1
1077

Existing issues

  1. In PHP code, use exec to execute nodejs command

  2. The Chinese characters received by nodejs are garbled characters

  3. All operations are run in CLI mode (that is, run under the command line)

  4. Executedchcp 65001Switch code page, and PHP and NodeJS files are UTF8 encoded (no BOM header)

  5. Run the NodeJS command directly from the command line, and the received value is normal

  6. When running PHP on the command line, the Chinese characters received by NodeJS are garbled

Development environment

  1. Windows 10 (1703 OS Build 15063.296)

  2. PHP 5.5.7 (cli)

  3. Zend Engine v2.5.0

  4. node v6.9.2

  5. Command line: cmd V10.0.15063

  6. Test script 1: test.php (php script)

  7. Test script 2: test.js (nodejs script)

  8. 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

Error recurrence

test.php

<?php
print(exec('node test.js -t "红掌拨清波"'));
?>

test.js

'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);

Reproduction process

  • 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

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
迷茫

Windows cmd defaults to gbk encoding, so the transmission of utf8 will be garbled

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template