Convert string separated with new lines to array in PHP
Use explode()
function with PHP_EOL
as a first argument.
explode(PHP_EOL, $array);
Example:
Names are separated with new lines. Use the explode()
function to parse them into an array.
<?php
$names = 'Vic
Peter
Amanda
Rick
Sophia';
$names = explode(PHP_EOL, $names);
var_dump($names);
?>
Output:
array(5) { [0]=> string(3) "Vic" [1]=> string(5) "Peter" [2]=> string(6) "Amanda" [3]=> string(4) "Rick" [4]=> string(6) "Sophia" }