我想在删除第一行后从文本区域收集数据并使用 php 回显。我需要在数据中添加一些单词
例如,下面的 php 是我处理文本区域 id 的 php 代码states
<?php
$text = $_POST['states'];
function getLines($text, $start, $end = false)
{
$devices = explode("\n", $text);
$output = "";
foreach ($devices as $key => $line)
{
if ($key+1 < $start) continue;
if ($end && $key+1 > $end) break;
$output .= $line;
}
return $output;
}
echo getLines($_POST['devs'], 2);
?>
所以如果我在我的文本区域输入以下信息
United State
Florida
Georgia
Illinois
New York
Texas
我会得到像这样的输出
Florida
Georgia
Illinois
New York
Texas
我需要像下面这样的输出来为first line
来自text area
其他文本的后缀
State Florida is in United State
State Georgia is in United State
State Illinois is in United State
State New York is in United State
State Texas is in United State
根据其他答案,我已更改 php 以添加前缀工作state
function getLines($text, $start, $end = false)
{
$devices = explode("\n", $text);
$prefix = "State ";
$suffix = echo " in $devices[0]";
$output = "";
foreach ($devices as $key => $line)
{
if ($key+1 < $start) continue;
if ($end && $key+1 > $end) break;
$output .= $prefix.$line.$suffix ;
}
return $output;
}
echo getLines($_POST['devs'], 2);
?>
但这不起作用。我可以单独添加前缀部分,
$output .= $prefix.$line;
以获得如下所示的输出
State Florida
如何将第一行与回声一起添加以显示输出
State Florida is in United States