0

我想在删除第一行后从文本区域收集数据并使用 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
4

1 回答 1

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);
于 2013-08-04T09:42:27.753 回答