- PHP

Iterate over each line with Foreach Loop in PHP

If you want to make a line by line text iteration in PHP, you can use explode with PHP_EOL and loop through all the lines with foreach or other loop.

$array = explode(PHP_EOL, $text);

Example:

<?php

$text = 'Michael
Anna
Hubert
Gina
Emma';

$array = explode(PHP_EOL, $text);

$i = 0;

foreach($array as $value) {
    if($i > 0) {
        echo ', ';
    }

    echo $value;

    $i++;
}

?>

Output: Michael, Anna, Hubert, Gina, Emma