2

My textarea ( $_POST['data'] ) contains 10 strings, each separated by a new line (\n). For example:

January
February
March
April
May
Jun
July
August
September
November

In PHP, how can I select only the first 5 elements from this $_POST['data']?

I tried:

$_POST['data'] = array_slice(explode("\n", $_POST['data']), 0, 5);

but it doesn't seem to work..

Tomasz
  • 1,288
  • 18
  • 36

1 Answers1

0

Try

<?php     
if (isset($_POST)){
$str = $_POST['data'];

$lines=explode("\n", $str);

for($i = 0; $i < 5; ++$i) {     
     echo $lines[$i];//just get the list
     echo "$lines[$i]"."<br>";//break the lines with br
     echo "$lines[$i]"."\n";//break the lines with nr
    }
 }
?>
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46