php-通过shell_exec使用utf-8文本输入来调用程序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-通过shell_exec使用utf-8文本输入来调用程序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1613字,纯文字阅读大概需要3分钟。
内容图文
![php-通过shell_exec使用utf-8文本输入来调用程序](/upload/InfoBanner/zyjiaocheng/950/1fe26dff65fb444eb458101d9db4a2d2.jpg)
资格:hunspell和php5.
来自bash的测试代码:
user@host ~/ $echo 'sagadījās' | hunspell -d lv_LV,en_US
Hunspell 1.2.14
+ sagadīties
-正常工作.
测试代码(test.php):
$encoding = "lv_LV.utf-8";
setlocale(LC_CTYPE, $encoding); // test
putenv('LANG='.$encoding); // and another test
$raw_response = shell_exec("LANG=$encoding; echo 'sagadījās' | hunspell -d lv_LV,en_US");
echo $raw_response;
回报
Hunspell 1.2.14
& sagad 5 0: tagad, sagad?ties, sagaudo, sagand?, sagar?o
*
*
屏幕截图(无法发布带有无效字符的代码):
似乎shell_exec无法正确处理utf-8,还是可能需要一些其他编码/解码?
编辑:我不得不使用en_US.utf-8来获取有效数据.
解决方法:
试试这个代码:
<?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);
?>
不要忘了验证文件的编码(以及因此传递的数据的编码)实际上是UTF-8
内容总结
以上是互联网集市为您收集整理的php-通过shell_exec使用utf-8文本输入来调用程序全部内容,希望文章能够帮你解决php-通过shell_exec使用utf-8文本输入来调用程序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。