dtvgo28624 2013-08-03 10:15
浏览 55
已采纳

如何使用php回显文本区域中的特定行

I am collecting html text area data to echo in php.I am able to select all data using

$devices = explode("
", $_POST['devs']);
foreach($devices as $device)
echo $device;

and I am able to select only the first line using:

$first_line = strstr(($_POST['devs']), "
", true);
echo $first_line;

But How can I echo specific lines ? say line 2 or 4 from text area ?

  • 写回答

7条回答 默认 最新

  • drj58429 2013-08-03 10:19
    关注

    Usage:

    getLines(YOUR POST, START LINE, END LINE(optional));
    

    With return array:

    function getLines($text, $start, $end = false)
    {
        $devices = explode("
    ", $text);
        $append  = "My device is ";
    
        $output = array();
        foreach ($devices as $key => $line)
        {
            if ($key+1 < $start) continue;
            if ($end && $key+1 > $end) break;
            $output[] = $append.$line;
        }
        return $output;
    }
    
    $array = getLines($_POST['devs'], 2);
    var_dump($array);
    

    With echo string:

    function getLines($text, $start, $end = false)
    {
        $devices = explode("
    ", $text);
        $append  = "My device is ";
    
        $output = "";
        foreach ($devices as $key => $line)
        {
            if ($key+1 < $start) continue;
            if ($end && $key+1 > $end) break;
            $output .= $append.$line."<br />";
        }
        return $output;
    }
    
    echo getLines($_POST['devs'], 2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?