我希望从文本区域收集数据并在删除第一行后使用php进行回显。我需要添加几个单词以及数据
例如,下面的php是我的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以添加前缀work 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
如何添加第一行和echo以显示输出
State Florida is in United States
答案 0 :(得分:1)
在这里。我不得不稍微修改它,因为你的模式我得到了1个不必要的线。
$text = "United State
Florida
Georgia
Illinois
New York
Texas
";
function getLines($text, $start, $end = false)
{
$devices = explode("\n", $text);
$prefix = "State ";
$suffix = " in ". $devices[0]."<br>";
$output = "";
$i = 0;
foreach ($devices as $key => $line)
{
$i++;
if ($key+1 < $start) continue;
if ($i != count($devices)) {
$output .= $prefix.$line.$suffix;
}
}
return trim(preg_replace('/\s\s+/', ' ', $output));
}
echo getLines($text, 2);