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条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作