2016-12-03 85 views
1

我有一個文本文件名爲1month.ssh警告:語法錯誤,意想不到的BOOL_TRUE

Host: m-sg5.portssh.com 
Username: portssh.com-myuser 
Password: mypass 
Port: 443 
Info: Your Account will expire on 02-January-2017 

我試圖根據與該文件創建一個數組:

$infossh = parse_ini_string(preg_replace('/^([^:]+): (.+)$/m','$1 = $2',file_get_contents('/home/ab/gconf/'.$_POST['account']))); 

$_POST['account']1month.ssh 。我的目標是當我呼應陣列,每個值應該是這樣的:

$infossh['Host'] = "m-sg5.portssh.com" 
$infossh['Username'] = "portssh.com-myuser" 
$infossh['Password'] = "mypass" 
$infossh['Port'] = "443" 
$infossh['Info'] = "Your Account will expire on 02-January-2017" 

而是使用代碼我得到以下錯誤:

Warning: syntax error, unexpected BOOL_TRUE in Unknown on line 5 in /www/ssh.php on line 218 

我該如何解決這個問題?

附加信息:

我有另外一個文件sgdo.ssh

Host: x-sgdo19.serverip.co 
Username: fastssh.com-myuser 
Password: mypass 
Port: 443 
Info: Date Expired : 10-December-2016 

,但我得到這個文件中沒有錯誤,錯誤,當我打開只發生1month.ssh

+0

你在同一個文件中有多個'Host'? – Perumal

+0

@ Perumal93不僅一個sgdo.ssh和1month.ssh是兩個不同的文件 – hillz

+0

我的意思是這些五行重複爲同一個文件中的每個主機?你有我的觀點? – Perumal

回答

1
Host: m-sg5.portssh.com 
Username: portssh.com-myuser 
Password: mypass 
Port: 443 
Info: Your Account will expire on 02-January-2017 

在這裏,Info的值應該用引號括起來。

編輯

$infossh = file('1month.ssh', FILE_IGNORE_NEW_LINES); 
$new_infossh = []; 

array_walk($infossh, function($element, $index) { 
    global $new_infossh; 
    $element_temp = explode(': ', $element); 
    $new_infossh[$element_temp[0]] = $element_temp[1]; 
}); 

var_dump($new_infossh); 

我修改我上面的代碼更簡單的一個。

$infossh = file('1month.ssh', FILE_IGNORE_NEW_LINES); 
$new_infossh = []; 

foreach($infossh as $value) { 
    $value_arr = explode(': ', $value); 
    $new_infossh[$value_arr[0]] = $value_arr[1]; 
} 

var_dump($new_infossh); 

在我看來,這些是其他兩種方式,你可以不使用正則表達式。希望它有效!

+0

修復它,但有沒有比這更好的方法? – hillz

+0

是的。但沒有正則表達。你想去嗎? – Perumal

+0

你可以編寫沒有正則表達式的代碼嗎? – hillz

相關問題