从 HTML 文本区域读取数据,然后将数据插入到 PHP 数组中
我一直在尝试创建一个跟踪器,到目前为止一切都很顺利,只是我正在尝试开发的一项新功能让我感到困惑。我试图拥有它,以便启动跟踪器的人可以输入名称,并且跟踪器将抓取用户输入的名称并查找它们。
我能想到的实现这一目标的唯一方法是插入 $_POST['names'] 中的名称并将它们插入到一个数组中,但是当我这样做时,它只会在数组中创建一个索引。
这是我正在使用的代码:
$lol = array();
$l = array($_POST['names']);
foreach ($l as $textarea) {
array_push($lol, nl2br($textarea));
}
for ($a = 0; $a < count($lol); $a++) {
echo "index " . $a . ": " . $lol[$a];
}
结果是:
index 0: lol
not
sure
how
this
will
work
我在带空格的文本字段中输入了上述内容。这是我正在使用的 HTML 文件:
<body>
<form name="form" action="test.php" method="GET">
<div align="center">
<textarea cols="13" rows="10" name="names"></textarea><br>
<input type="submit" value="lol">
</div>
</form>
</body>
所以我想我要问的是,是否可以让人在文本框中输入“Ron”、“Mark”、“Tommi”和“Daniel”,我可以插入“Ron”、“将“Mark”、“Tommi”和“Daniel”放入一个数组中,以便它们的索引分别为 [0, 1, 2, 3],如果是这样,我将如何实现这一点?
I've been trying to create a tracker and everything is going smoothly thus far, just one new feature I'm trying to work on has me stumped. I'm trying to have it so the person who starts a tracker can enter names and the tracker will grab the names the user entered and look them up.
The only way that I can think of accomplishing this would be to insert the names from $_POST['names'] and insert them into an array, but when I do this, it only creates one index inside the array.
This is the code that I'm using:
$lol = array();
$l = array($_POST['names']);
foreach ($l as $textarea) {
array_push($lol, nl2br($textarea));
}
for ($a = 0; $a < count($lol); $a++) {
echo "index " . $a . ": " . $lol[$a];
}
The result of that is:
index 0: lol
not
sure
how
this
will
work
I typed the above out in the text field with spaces. Here's the HTML file that I'm using as well:
<body>
<form name="form" action="test.php" method="GET">
<div align="center">
<textarea cols="13" rows="10" name="names"></textarea><br>
<input type="submit" value="lol">
</div>
</form>
</body>
So I guess what I'm asking is, is it possible to have a person put, say, "Ron", "Mark", "Tommi", and "Daniel" in the text box, could I insert "Ron", "Mark", "Tommi" and "Daniel" into an array so that their index would be [0, 1, 2, 3] respectively, and if so how would I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
explode
将一个字符串拆分为另一个子字符串。Use
explode
to split a string by another substring.