fork(2) download
  1. <?php
  2.  
  3. $str = "My text1\nMy text2\nMy text3";
  4. $arr = explode("\n", $str);
  5.  
  6. $array = array(); // inisiasi variable array in format array
  7.  
  8. foreach ($arr as $line) { // loop line by line and convert into array
  9. $array[] = $line;
  10. };
  11.  
  12. print_r($array); // display all value
  13.  
  14. echo $array[1]; // diplay index 1
  15.  
  16. echo PHP_EOL;
  17.  
  18. foreach ($arr as $line_num => $line) {
  19. echo "Line #{$line_num} : " . htmlspecialchars($line) . PHP_EOL;
  20. }
  21.  
  22. ?>
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Array
(
    [0] => My text1
    [1] => My text2
    [2] => My text3
)
My text2
Line #0 : My text1
Line #1 : My text2
Line #2 : My text3