如何保存与PHP .txt文件内textarea的内容 [英] How to save textarea content inside a .txt file with php
问题描述
我是用PHP初学者,我试图挽救一个news.txt文件中textarea的内容。
i'm a beginner with php, and i'm trying to save textarea content inside a news.txt file.
保存后,我会显示另一页的结果。
一切工作正常,在ddate和完美显示的标题,甚至textarea的内容,但如果我preSS输入来创建一个新的生产线,它不工作了。
After saving, i'll display the results in another page. Everything works fine, the ddate and the title is displayed perfectly, and even the textarea content, but if i press enter to create a new line, it doesn't work anymore.
下面就是发生一些code。
here's what happen and some code.
注意:未定义抵消:1在C:\\虚拟服务器\\ XAMPP \\ htdocs中\\ read.php第17行
Notice: Undefined offset: 1 in C:\Virtual Servers\xampp\htdocs\read.php on line 17
注意:未定义抵消:2在C:\\虚拟服务器\\ XAMPP \\ htdocs中\\ read.php在线18
Notice: Undefined offset: 2 in C:\Virtual Servers\xampp\htdocs\read.php on line 18
和我的code:
read.php页
<html>
<head>
<style>
.news{background:#f1f1f1;width:960px;height:500px;margin-bottom:15px;}
.title{color:red;}
.date{font-style:italic;}
.text{color:green;font-weight:bold;}
</style>
</head>
<body>
<?php
$i = 0;
$array = file("news/news.txt");
foreach($array as $row){
$split = explode('|', "$row");
$data = $split[0];
$titolo = $split[1];
$testo = $split[2];
$i++;
print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
}
?>
</body>
</html>
index.php页面
index.php page:
<div class="container">
<form name="myform" onsubmit="return validateForm()" action="welcome.php" method="post">
<?php echo date('d-m-Y'); ?></br>
<h1>Titolo:</h1><input type="text" id="Title" name="titolo" class="textStyleTitle"><br>
<p>Testo:</p><textarea id="Text" name="testo" class="textStyleText"></textarea><br>
<input type="submit" value="salva">
</form>
</div>
任何解决方案?
非常感谢。
!! UPDATE!
通过该解决方案下面提到的,我已经解决了一部分。我没有错误,但这里是它的样子:
!!UPDATE!! With the solution mentioned below, i've solved in part. I have no errors, but here's how it looks like:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)</div>
</div>
<div id="2" class="news">
<div class="date">Testing textarea row 2 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="3" class="news">
<div class="date">Testing textarea row 3 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="4" class="news">
<div class="date">Testing textarea row 4</div>
<div class="title"></div><div class="text"></div>
</div></body>
unfortunally,我试着去达成是这样的:
unfortunally, what i'm tryin' to reach is something like this:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)
Testing textarea row 2 (enter pressed)
Testing textarea row 3 (enter pressed)
Testing textarea row 4
</div>
</div>
推荐答案
先删除$报价行的爆炸使得如下: $拆分=爆炸('|',$行);
然后
first remove quotes for $row in explode make as below:
$split = explode('|', $row);
then
使用:
$data = (isset($split[0])?$split[0]:"");
而不是
$data = $split[0];
同为 $拆分[1]
和 $分割[2]
这篇关于如何保存与PHP .txt文件内textarea的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!