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

Asked

Viewed 1,784 times

3

I’m picking up a PHP file.

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

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

for example:

Nome 1
Nome 2
Nome 3
Nome 4

I’ll get these 4 names in a file .txt and put them in a array. The goal is to put all the information into one vector and write it.

2 answers

4


You can use the function explode() for that reason:

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

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

echo nl2br($arquivo);

foreach($nomes as $nome){
    echo $nome;
}
  • How do I write the variables?

  • With print_r or use a foreach to iterate the results. You can also reference row by row using the array $nomes[0]

3

Use the function file(), it reads the selected file to 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";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.