search

Home  >  Q&A  >  body text

node.js - When PHP uses exec to execute nodejs, the Chinese characters passed into JS are garbled

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


怪我咯怪我咯2762 days ago1098

reply all(1)I'll reply

  • 迷茫

    迷茫2017-06-30 09:55:51

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

    reply
    0
  • Cancelreply