Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php从文本区域回显特定行_Php_Html - Fatal编程技术网

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

如何使用php从文本区域回显特定行,php,html,Php,Html,我正在收集html文本区域数据,以便在php中进行回显 $devices = explode("\n", $_POST['devs']); foreach($devices as $device) echo $device; 我可以使用以下命令仅选择第一行: $first_line = strstr(($_POST['devs']), "\n", true); echo $first_line; 但我如何回应特定的线路?从文本区域说出第2行或第4行?这样做 $nth_line = explo

我正在收集html文本区域数据,以便在php中进行回显

$devices = explode("\n", $_POST['devs']);
foreach($devices as $device)
echo $device;
我可以使用以下命令仅选择第一行:

$first_line = strstr(($_POST['devs']), "\n", true);
echo $first_line;
但我如何回应特定的线路?从文本区域说出第2行或第4行?

这样做

$nth_line = explode("\n", $_POST['devs'])[n];
你的电话号码是多少

explode()返回一个数组,然后可以通过基本数组操作选择每个元素
进一步的读数

因为分解后,
$devices
是一个数组,所以可以根据它的索引处理每一行。提醒:数组是基于零索引的,因此1从0开始

$devices = explode('\n', $_POST['devs']);

// line 1
echo $devices[0];

// line 2
echo $devices[1];

// line 4
echo $devices[3];

在php.net上查找语法。是的

$devices = explode(";", "aap;noot;mies");
print_r($devices);
   foreach ($devices as $key => $value) {
     echo "<br>nr.$key=" .  $devices[$key];
   }
$devices=explode(“;”,“aap;noot;mies”);
打印设备;
foreach($key=>$value的设备){
echo“
nr.$key=”.$devices[$key]; }
您的第一个代码段已经通过函数创建了一个行数组

因此,要输出第2行和第4行,只需使用:

$devices = explode("\n", $_POST['devs']);
echo $devices[1];
echo $devices[3];
如果您是PHP新手(我猜这是由于您问题的性质),应该注意的是,与许多编程语言一样,数组从零开始索引,因此第2行是,第4行是[3],等等

更新

要访问倒数第二行(即:倒数第二行),您可以使用:

echo $devices[count($devices) - 2];
我们在这里做的是获取数组中的元素数(via),然后减去2以获取最后一个元素。(因为我们需要减去1来处理数组从零开始索引的事实。)

您可以使用拆分:

$lines = split("\n", $_POST['devs']);

echo $lines[3];  //4th line
请参阅文档

用法:

getLines(YOUR POST, START LINE, END LINE(optional));
带返回数组:

function getLines($text, $start, $end = false)
{
    $devices = explode("\n", $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);
function getLines($text, $start, $end = false)
{
    $devices = explode("\n", $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);
函数getLines($text,$start,$end=false) { $devices=explode(“\n”,$text); $append=“我的设备是”; $output=array(); foreach($key=>$line形式的设备) { 如果($key+1<$start)继续; 如果($end&&$key+1>$end)中断; $output[]=$append.$line; } 返回$output; } $array=getLines($_POST['devs'],2); 变量转储($数组); 带有回显字符串:

function getLines($text, $start, $end = false)
{
    $devices = explode("\n", $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);
function getLines($text, $start, $end = false)
{
    $devices = explode("\n", $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);
函数getLines($text,$start,$end=false) { $devices=explode(“\n”,$text); $append=“我的设备是”; $output=“”; foreach($key=>$line形式的设备) { 如果($key+1<$start)继续; 如果($end&&$key+1>$end)中断; $output.=$append.$line.“
”; } 返回$output; } echo getLines($_POST['devs'],2);
看看PHP中的数组操作。由于
$devices
是一个数组,您可以通过其索引选择一个元素,如下所示:
$devices[1]
用于第二个元素,
$devices[2]
用于第三个元素等。

echo“第二行:\n”、$devices[1]、“\n第三行:\n”、$devices[3]
。请注意,只有PHP5.4+PHP4及以上版本支持explode,才支持此语法。我指的是在同一行中访问索引
n
。PHP5.3及以下版本不支持这一点。你能进一步解释你的答案吗?@middaparka:谢谢你解释清楚。(我是PHP新手)。假设,如果我想选择最后一行的第二行,我们将如何提及最后一行?我的意思是,我们真的不知道有多少行,但需要从第二行到最后一行进行选择。@middaparka:最后一个问题。我们如何选择一系列设备?从2到最后的示例?。我想知道如何将它们一起调用。(类似于我们使用foreach函数的方式,没有提到每个函数都带有“$devices[n]”)。@acr最简单的方法是使用正常的
for
循环,而不是我想的foreach。我如何使用它来选择范围?从第二行到最后一行?其中无法计算行数predicted@acr以
0
开头的数组索引。例如,它的意思是:获取第3行时,您必须使用
echo$devices[2]@acr对于使用范围,你可以使用
foreach
@Bora我知道了。我的问题是假设我有10行,我想显示第2行到最后10行。在上面的例子中,我已经逐行提到了$devices[1]到$devices[9]。但在我的情况下,我不确定会有多少行。这取决于访问者进入我的文本区域的行数。所以我想要的是,我需要从第2行到第n行进行回音,其中n是我的最后一个行号。每件东西function@acr等一下。要将所有行输出为回显字符串或数组吗?