如何使用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 ?
我正在收集html文本区域数据以在php中回显。我能够使用 p>选择所有数据
$ devices = explode(“
”,$ _POST ['devs']);
foreach($ devices as $ device)
echo $ device;
code>
我只能使用以下方法选择第一行: p>
$ first_line = strstr(($ _ POST ['devs']) ,“
”,true);
echo $ first_line;
code> pre>
但我如何回显特定的行? 从文本区域说出第2行或第4行? p>
div>
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);
Do it like this
$nth_line = explode("
", $_POST['devs'])[n];
where n is you line no.
the explode() returns an array then you can select each element by basic array operation further readings http://php.net/manual/en/function.explode.php
because $devices
is an array after exploding it, you can treat each line by it's index. Reminder that arrays are zero-index based so 1 starts at 0.
$devices = explode('
', $_POST['devs']);
// line 1
echo $devices[0];
// line 2
echo $devices[1];
// line 4
echo $devices[3];
Lookup your syntax on php.net. It is
$devices = explode(";", "aap;noot;mies");
print_r($devices);
foreach ($devices as $key => $value) {
echo "<br>nr.$key=" . $devices[$key];
}
Your first code snippet is already creating an array of lines via the explode function.
As such, to output the 2nd and 4th lines, you can simply use:
$devices = explode("
", $_POST['devs']);
echo $devices[1];
echo $devices[3];
If you're new to PHP (I'm guessing this is the case due to the nature of your question), it should be noted that like many programming languages, arrays are indexed from zero, hence line 2 is 1, line 4 is [3], etc.
UPDATE
To access the penultimate (i.e.: 2nd to last) line, you could use:
echo $devices[count($devices) - 2];
What we're doing here is getting the number of elements in the array (via count) and then subtracting two to fetch the second last element. (As we need to subtract one to deal with the fact that arrays are indexed from zero.)
you can use split:
$lines = split("
", $_POST['devs']);
echo $lines[3]; //4th line
See documentation http://php.net/manual/es/function.split.php
Take a look at array operations in PHP. Since $devices
is an array you can select an element by its index like this: $devices[1]
for second element, $devices[2]
for third etc.