從PHP

2017-07-20 24 views
1

選項shell命令我有這樣的代碼:從PHP

$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,結果相同。 所以問題是,我做錯了什麼以及如何解決它?謝謝!

回答

1

您在$ codif變量的末尾,它弄亂有一個新行事情了。 需要刪除某些方法。 你可以試試這個,例如:file命令

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

管道輸出到tr -d "\n"將刪除新行。你當然也可以在php代碼中刪除它。

+0

是的,你打它!非常感謝! –

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

+0

對不起,我沒有得到你之前,是更改爲固定值使其工作,但我需要編碼檢測部分,所以這就是失敗。 –

+0

無論如何,我也試着改變'--from-code ='到'-f',它不起作用,但是將'$ codif'變量修改爲一個固定值'iso-8859-1'使它工作。所以這個問題似乎正在得到執行de commad的變量值。 –