Is there a way to seperate them to array like
0:Apple,Orange,Watermelon
1:One,Two,Three
3:Dog,Cat,Mouse
$lines = array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $decode) as $line){
array_push($lines,$line);
}
CodePudding user response:
$str="apple\r\norange\r\nmango\r\npomegranate\r\n\r\ncat\r\ndog\r\nhorse\r\nhippopotamus\r\ngiraffe\r\nrabidbehampstergbabble\r\n\r\nmale\r\nfemale\r\nplate\r\n";
# is essentially the same as
/*
apple
orange
mango
pomegranate
cat
dog
horse
hippopotamus
giraffe
rabidbehampstergbabble
male
female
plate
*/
// initially break apart on designated boundary \r\n\s
$a=array_filter( preg_split('@(\r\n\s)@', trim($str) ) );
// process each portion of the initial array
$a=array_map( function( $n ){
// trim the item and return an imploded string after splitting on newline
return implode( ',', array_filter( preg_split( '@(\r\n)@', trim( $n ) ) ) );
},$a );
printf('<pre>%s</pre>',print_r($a,1));
Which yields:
Array
(
[0] => apple,orange,mango,pomegranate
[1] => cat,dog,horse,hippopotamus,giraffe,rabidbehampstergbabble
[2] => male,female,plate
)