如何使用php在textarea中保存换行符?目前我有这个临时解决方案,通过使用exec
执行shell字符串来完成工作,但如果可能的话,我希望它全部都在php中。这是我的临时脚本,请告诉我应该如何编辑
<?php
include 'theme.php';
/* ceklogin(); */
css();
if ($_POST['wget-send']) {
$formdir = $_POST['dir'];
$formlink = $_POST['link'];
$filelink = fopen('/root/wget/wget-download-link.txt', a);
$filedir = fopen('/root/wget/wget-dir.txt', w);
fwrite($filedir, $formdir);
exec('echo "' . $formlink . '" >> /root/wget/wget-download-link.txt', $out);
echo $out[2];
exit();
}
if ($_POST['restart-download']) {
exec('mv /root/wget/wget-download-link.txt.done /root/wget/wget-download-link.txt');
exit();
}
echo "<form action=\"" . $PHP_SELF . "\" method=\"post\" id=\"WgetForm\">";
echo "Download directory:<br><input type=\"text\" name=\"dir\" size=\"15\" value=\"/mnt/usb/\"/> <br>";
echo '<br>Download link:';
echo ("<textarea name=\"link\" rows=\"13\" cols=\"62\"></textarea><br>");
echo '<input type="submit" onclick="LinkAdded()" name="wget-send" value="Download" id="WgetID"/><br>';
echo '<input type="submit" onclick="DownloadRestarted()" name="restart-download" value="Restart latest download" id="RestartWget"/>';
echo "</form></div>";
echo <<<HTML
<script type="text/javascript">
function LinkAdded(){
alert("Link has been sucessfully sent to wget, it'll be downloaded soon, check the wget log for the download progress");
}
function DownloadRestarted(){
alert("Download restarted, check the wget log for the download progress");
}
</script>
HTML;
foot();
echo '
</div>
</body>
</div>
</html>';
?>
如果我使用nl2br
,输出文件将是这样的
satu<br />
dua<br />
tiga<br />
empat<br />
lima
我不希望那些<br />
在那里。这是包含nl2br
的脚本:
<?php
include 'theme.php';
/*ceklogin();*/
css();
if($_POST['wget-send'])
{
$formdir=$_POST['dir'];
$formlink=$_POST['link'];
$filelink = fopen('/root/wget/test.txt',a);
$filedir = fopen('/root/wget/wget-dir.txt',w);
$savelink = nl2br($formlink);
fwrite($filedir, $formdir);
fwrite($filelink, $savelink);
}
if($_POST['restart-download'])
{
exec('mv /root/wget/wget-download-link.txt.done /root/wget/wget-download-link.txt');
exit();
}
echo "<form action=\"".$PHP_SELF."\" method=\"post\" id=\"WgetForm\">";
echo "Download directory:<br><input type=\"text\" name=\"dir\" size=\"15\" value=\"/mnt/usb/\"/><br>";
echo '<br>Download link:';
echo ("<textarea name=\"link\" rows=\"13\" cols=\"62\"></textarea><br>");
echo '<input type="submit" onclick="LinkAdded()" name="wget-send" value="Download" id="WgetID"/><br>';
echo '<input type="submit" onclick="DownloadRestarted()" name="restart-download" value="Restart latest download" id="RestartWget"/>';
echo "</form></div>";
echo <<<HTML
<script type="text/javascript">
function LinkAdded()
{
alert("Link has been sucessfully sent to wget, it'll be downloaded soon, check the wget log for the download progress");
}
function DownloadRestarted()
{
alert("Download restarted, check the wget log for the download progress");
}
</script>
HTML;
foot();
echo '
</div>
</body>
</div>
</html>';
?>
注意我希望文件test.txt
的输出如下所示<{1}}部分
<br />
答案 0 :(得分:0)
将textarea保存到数据库或文本文件时使用nl2br。然后你就可以在textarea中回显它:
<?php
// Input (can be saved in database or text file)
$toSave = nl2br($_POST["link"]);
// Output (from databse or text file)
echo "<textarea name=\"link\" rows=\"13\" cols=\"62\">$output</textarea><br>";
?>