2017-02-18 95 views
1

我有一個腳本,它工作正常。但是輸出是多行的。我怎樣才能解決這個問題,以輸出到一個單一的行?如何在一行中回顯輸出?

<?php 
$filename1 = './workspace/vars1.txt'; 
$contents1 = file($filename1); 
foreach ($contents1 as $line1) { 
    $str1 = " <TR><TD>$line1:</TD><TD> <input type=\"text\"name \"$line1\" " ; 
    echo $str1; 
    fwrite($file, $str1); 
} 
fclose($file); 
?> 

輸出:

<TR><TD>ID 
:</TD><TD> <input type="text"name="ID 
" <TR><TD>APP 
:</TD><TD> <input type="text"name="APP 
" 

輸出應該是:

<TR><TD>ID:</TD><TD> <input type="text"name="ID" <TR><TD>APP:</TD><TD> <input type="text"name="APP" 
+0

這些值很可能包含換行符。嘗試使用'trim()'函數。 – arkascha

回答

0

可能是你的價值包含空格。修剪然後顯示它。

foreach ($contents1 as $line1) { 
$line1 = trim($line1); 
$str1 = " <TR><TD>$line1:</TD><TD> <input type=\"text\"name=\"$line1\" " ; 
echo $str1; 
fwrite($file, $str1); 
} 
0

file()函數讀取文件中的行時,它還包含新的行字符。我們只需要刪除它們:

foreach ($contents1 as $line1) { 
    // Remove any new line characters from the string before using it. 
    $line1 = str_replace(PHP_EOL, '', $line1); 
    $str1 = " <TR><TD>$line1:</TD><TD> <input type=\"text\"name=\"$line1\" " ; 
    echo $str1; 
    fwrite($file, $str1); 
} 
相關問題