带有php选项的shell命令

时间:2017-07-20 09:34:25

标签: php shell command iconv

我有这段代码:

$codif ="file --mime-encoding -b inputfile.txt";
$codif = shell_exec($codif);
$encode = "iconv --from-code=$codif --to-code=UTF-8 --output=tempfile.txt inputfile.txt";

我试过了

shell_exec($encode); //1
exec($encode); //2
system($encode); //3

我有这个只是为了看看生成的命令是什么:

echo $encode;

哪个输出:

iconv --from-code=iso-8859-1 --to-code=UTF-8 --output=tempfile.txt inputfile.txt

问题在于,执行命令的三种形式之一我得到下一个错误:

sh: 2: --to-code=UTF-8: not found

在shell上执行输出命令时效果很好。 我也尝试将--to-code=UTF-8更改为-t UTF-8并获得相同的结果。 所以问题是,我做错了什么以及如何解决它?谢谢!

2 个答案:

答案 0 :(得分:1)

你在$ codif变量的末尾有一个新行,这会让事情变得混乱。 这需要以某种方式剥离。 你可以试试这个例子:

$codif ="file --mime-encoding -b inputfile.txt|tr -d '\n'";
$codif = shell_exec($codif);

file命令的输出输出到tr -d "\n"将删除新行。你当然可以在php代码中删除它。

答案 1 :(得分:0)

尝试将$encode var更改为:

iconv -f $codif -t UTF-8 --output=tempfile.txt inputfile.txt

输出如下内容:

iconv -f iso-8859-1 -t UTF-8 --output=tempfile.txt inputfile.txt