Split a string on newlines in PHP
The task is to split that string and store them into array such that strings are splitted by the newline. Using explode() Function: The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs.
php explode new line
$values = preg_split('/\r\n|\r|\n/', $myString);
php split string by enter
$your_array = explode("\n", $your_string);
Example
<?php
// A php program to separate string
// using preg_split() function
// String which to be converted
$str = "Ankit Mishra\nRam Singh\nShyam Pandey";
// This function converts the string
$arr= preg_split ('/\n/', $str);
// print the information of array
print_r($arr);
?>
Output
Array
(
[0] => Ankit Mishra
[1] => Ram Singh
[2] => Shyam Pandey
)
(
[0] => Ankit Mishra
[1] => Ram Singh
[2] => Shyam Pandey
)