我的代码需要帮助,我想解析已执行的ssh2命令的输出并将其输出到csv中,稍后再将csv插入到mysql表中。
问题是我无法将结果分成几列(我在mysql中只得到一列)。
这是代码:
include('Net/SSH2.php');
$ssh_out=$ssh->exec('show cable modem phy');
echo "<pre>$ssh_out</pre>";
$linez=explode("\n",$ssh_out);
var_dump($linez);
echo implode(",", $linez );
$myfile = fopen("tmp.txt", "w") or die("Unable to open file!");
$txt = "$ssh_out\n"; //"$ssh_out\n"
var_dump($ssh_out);
fwrite($myfile, $txt);
fclose($myfile);
$handle = fopen("tmp.txt", "r");
$lines = [];
if (($handle = fopen("tmp.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$lines[] = $data;
}
fclose($handle);
}
$fp = fopen('snr.csv', 'w');
foreach ($lines as $line) {
fputcsv($fp, $line);
}
fclose($fp);
从csv中的代码重排:
"5009.59e2.7b75 C5/0/0/U0 1 53.00 35.40 2410 0.30 39.70 atdma* 1.1"
我需要什么:"5009.59e2.7b75","C5/0/0/U0","1","53.00","35.40","2410","0.30","39.70","atdma* 1.1"
我哪里出错了?