Catch the line break in a file and put the content in a variable

3

I'm getting a file in PHP.

$arquivo = file_get_contents($_FILES['arquivo']['tmp_name']);

I want to know how do I get this file and put it in an array for every line break that it finds \ n .

For example:

Nome 1
Nome 2
Nome 3
Nome 4

I'll get these 4 names in a .txt file and put them in an array . The goal is to put all information in a vector and write them down.

    
asked by anonymous 25.05.2015 / 15:59

2 answers

4

You can use the explode() function to do this:

$nomes = explode("\n", $arquivo);

If you just want to view the data, you can use nl2br() or iterate the results:

echo nl2br($arquivo);

foreach($nomes as $nome){
    echo $nome;
}
    
25.05.2015 / 16:05
3

Use the file() function, it reads the selected file into an array.

$lines = file ('arquivo');

// Percorre o array, mostrando o número da linha que está
foreach ($lines as $line_num => $line) {
    echo "Linha #<b>{$line_num+1}</b> : {$line}<br>\n";
}
    
25.05.2015 / 16:09