PHP exec change encoding

后端 未结 3 1631
花落未央
花落未央 2020-12-14 18:46

I need to address UTF-8 filenames with the php exec command. The problem is that the php exec command does not seem to understand utf-8. I use something like th

相关标签:
3条回答
  • 2020-12-14 19:27

    To answer my own question - i found the following solution:

    setting the locale environment variable with PHP

    $locale='de_DE.UTF-8';
    setlocale(LC_ALL,$locale);
    putenv('LC_ALL='.$locale);
    echo exec('locale charmap');
    

    This sets to / returns UTF-8. So i'm able to pass special characters and umlauts to linux shell commands.

    0 讨论(0)
  • 2020-12-14 19:31

    I had the similar problem. My program was returning me some German letters like: üäöß. Here is my code:

    $programResult = shell_exec('my script');
    

    Variable $programResult is containing German umlauts, but they were badly encoded. In order to encode it properly you can call utf8_encode() function.

    $programResult = shell_exec('my script');
    $programResult = utf8_encode($programResult);
    
    0 讨论(0)
  • 2020-12-14 19:36

    This solves it for me (source: this comment here):

    <?php
    putenv('LANG=en_US.UTF-8'); 
    $command = escapeshellcmd('python3 myscript.py');
    $output = shell_exec($command);
    echo $output;
    ?>
    
    0 讨论(0)
提交回复
热议问题