Call a program via shell_exec with utf-8 text input

前端 未结 1 1735
我在风中等你
我在风中等你 2021-01-16 12:41

Perquisites: hunspell and php5.

Test code from bash:

user@host ~/ $ echo \'sagadījās\' | hunspell -d lv_LV,en_US
Hunspell 1         


        
相关标签:
1条回答
  • 2021-01-16 13:12

    Try this code:

    <?php
    
      // The word we are checking
      $subject = 'sagadījās';
    
      // We want file pointers for all 3 std streams
      $descriptors = array (
        0 => array("pipe", "r"),  // STDIN
        1 => array("pipe", "w"),  // STDOUT
        2 => array("pipe", "w")   // STDERR
      );
    
      // An environment variable
      $env = array(
        'LANG' => 'lv_LV.utf-8'
      );
    
      // Try and start the process
      if (!is_resource($process = proc_open('hunspell -d lv_LV,en_US', $descriptors, $pipes, NULL, $env))) {
        die("Could not start Hunspell!");
      }
    
      // Put pipes into sensibly named variables
      $stdIn = &$pipes[0];
      $stdOut = &$pipes[1];
      $stdErr = &$pipes[2];
      unset($pipes);
    
      // Write the data to the process and close the pipe
      fwrite($stdIn, $subject);
      fclose($stdIn);
    
      // Display raw output
      echo "STDOUT:\n";
      while (!feof($stdOut)) echo fgets($stdOut);
      fclose($stdOut);
    
      // Display raw errors
      echo "\n\nSTDERR:\n";
      while (!feof($stdErr)) echo fgets($stdErr);
      fclose($stdErr);
    
      // Close the process pointer
      proc_close($process);
    
    ?>
    

    Don't forget to verify that the encoding of the file (and therefore the encoding of the data you are passing) actually is UTF-8 ;-)

    0 讨论(0)
提交回复
热议问题